Mercurial > lasercutter
view src/clojure/contrib/profile.clj @ 10:ef7dbbd6452c
added clojure source goodness
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 21 Aug 2010 06:25:44 -0400 |
parents | |
children |
line wrap: on
line source
1 ;;; profile.clj: simple code profiling & timing3 ;; by Stuart Sierra, http://stuartsierra.com/4 ;; May 9, 20096 ;; Copyright (c) Stuart Sierra, 2009. All rights reserved. The use7 ;; and distribution terms for this software are covered by the Eclipse8 ;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)9 ;; which can be found in the file epl-v10.html at the root of this10 ;; distribution. By using this software in any fashion, you are11 ;; agreeing to be bound by the terms of this license. You must not12 ;; remove this notice, or any other, from this software.15 (ns ^{:author "Stuart Sierra"16 :doc "Simple code profiling & timing measurement.18 Wrap any section of code in the prof macro, giving it a name, like this:20 (defn my-function [x y]21 (let [sum (prof :addition (+ x y))22 product (prof :multiplication (* x y))]23 [sum product]))25 The run your code in the profile macro, like this:27 (profile (dotimes [i 10000] (my-function 3 4)))29 Which prints a report for each named section of code:31 Name mean min max count sum32 addition 265 0 37000 10000 265500033 multiplication 274 0 53000 10000 274700035 Times are measured in nanoseconds, to the maximum precision available36 under the JVM. See the function documentation for more details.37 "}38 clojure.contrib.profile)40 (def *profile-data* nil)42 (def ^{:doc "Set this to false before loading/compiling to omit43 profiling code."} *enable-profiling* true)45 (defmacro prof46 "If *enable-profiling* is true, wraps body in profiling code.47 Returns the result of body. Profile timings will be stored in48 *profile-data* using name, which must be a keyword, as the key.49 Timings are measured with System/nanoTime."50 [name & body]51 (assert (keyword? name))52 (if *enable-profiling*53 `(if *profile-data*54 (let [start-time# (System/nanoTime)55 value# (do ~@body)56 elapsed# (- (System/nanoTime) start-time#)]57 (swap! *profile-data* assoc ~name58 (conj (get @*profile-data* ~name) elapsed#))59 value#)60 ~@body)61 `(do ~@body)))63 (defmacro with-profile-data64 "Executes body with *profile-data* bound to an atom of a new map.65 Returns the raw profile data as a map. Keys in the map are profile66 names (keywords), and values are lists of elapsed time, in67 nanoseconds."68 [& body]69 `(binding [*profile-data* (atom {})]70 ~@body71 @*profile-data*))73 (defn summarize74 "Takes the raw data returned by with-profile-data and returns a map75 from names to summary statistics. Each value in the map will look76 like:78 {:mean ..., :min ..., :max ..., :count ..., :sum ...}80 :mean, :min, and :max are how long the profiled section took to run,81 in nanoseconds. :count is the total number of times the profiled82 section was executed. :sum is the total amount of time spent in the83 profiled section, in nanoseconds."84 [profile-data]85 (reduce (fn [m [k v]]86 (let [cnt (count v)87 sum (reduce + v)]88 (assoc m k {:mean (int (/ sum cnt))89 :min (apply min v)90 :max (apply max v)91 :count cnt92 :sum sum})))93 {} profile-data))95 (defn print-summary96 "Prints a table of the results returned by summarize."97 [profile-summary]98 (let [name-width (apply max 1 (map (comp count name) (keys profile-summary)))99 fmt-string (str "%" name-width "s %8d %8d %8d %8d %8d%n")]100 (printf (.replace fmt-string \d \s)101 "Name" "mean" "min" "max" "count" "sum")102 (doseq [k (sort (keys profile-summary))]103 (let [v (get profile-summary k)]104 (printf fmt-string (name k) (:mean v) (:min v) (:max v) (:count v) (:sum v))))))106 (defmacro profile107 "Runs body with profiling enabled, then prints a summary of108 results. Returns nil."109 [& body]110 `(print-summary (summarize (with-profile-data (do ~@body)))))