Created
February 23, 2011 08:35
-
-
Save rik0/840175 to your computer and use it in GitHub Desktop.
bench.clj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns lazier.bench | |
(:use clojure.core | |
[clojure.contrib.str-utils :only (re-sub str-join)] | |
clojure.contrib.pprint)) | |
(defn slow-computation [& more] | |
(Thread/sleep 500) | |
(if (seq more) more 1)) | |
(defmacro benchmark | |
[functions lst] | |
(println ";" (str-join "" (repeat 72 \=))) | |
(pprint `~lst) | |
(letfn [(make-fragment [f] `(~f ~lst))] | |
`(do | |
~@(loop [functions functions timers []] | |
(if (seq functions) | |
(recur (rest functions) | |
(cons `(do | |
(print ";" (quote ~(first functions)) ": ") | |
(time ~(make-fragment (first functions))) | |
nil) | |
timers)) | |
timers)) | |
(println)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(list (slow-computation)) | |
(list (slow-computation) (slow-computation))) | |
; seq : "Elapsed time: 1500.213 msecs" | |
; next : "Elapsed time: 1500.26 msecs" | |
; rest : "Elapsed time: 1500.362 msecs" | |
; first : "Elapsed time: 1500.136 msecs" | |
; seq? : "Elapsed time: 1500.369 msecs" | |
; list? : "Elapsed time: 1500.233 msecs" | |
; empty? : "Elapsed time: 1500.242 msecs" | |
; identity : "Elapsed time: 1500.169 msecs" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(benchmark** [identity empty? list? seq? first rest next seq] | |
(concat (list (slow-computation)) | |
(list (slow-computation) (slow-computation)))) | |
; ======================================================================== | |
(concat | |
(list (slow-computation)) | |
(list (slow-computation) (slow-computation))) | |
; seq : "Elapsed time: 0.019 msecs" | |
; next : "Elapsed time: 0.028 msecs" | |
; rest : "Elapsed time: 0.02 msecs" | |
; first : "Elapsed time: 0.024 msecs" | |
; seq? : "Elapsed time: 0.012 msecs" | |
; list? : "Elapsed time: 0.02 msecs" | |
; empty? : "Elapsed time: 0.025 msecs" | |
; identity : "Elapsed time: 0.011 msecs" | |
nil | |
lazier.core> (benchmark* [identity empty? list? seq? first rest next seq] | |
(concat (list (slow-computation)) (list (slow-computation) (slow-computation)))) | |
; ======================================================================== | |
(concat | |
(list (slow-computation)) | |
(list (slow-computation) (slow-computation))) | |
; seq : "Elapsed time: 1500.205 msecs" | |
; next : "Elapsed time: 1500.356 msecs" | |
; rest : "Elapsed time: 1500.212 msecs" | |
; first : "Elapsed time: 1500.307 msecs" | |
; seq? : "Elapsed time: 1500.311 msecs" | |
; list? : "Elapsed time: 1500.281 msecs" | |
; empty? : "Elapsed time: 1500.171 msecs" | |
; identity : "Elapsed time: 1500.275 msecs" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(benchmark [identity empty? list? seq? first rest next seq] | |
(concat (lazy-seq (list (slow-computation))) | |
(lazy-seq (list (slow-computation) | |
(slow-computation))))) | |
; ======================================================================== | |
(concat | |
(lazy-seq (list (slow-computation))) | |
(lazy-seq (list (slow-computation) (slow-computation)))) | |
; seq : "Elapsed time: 500.431 msecs" | |
; next : "Elapsed time: 1500.505 msecs" | |
; rest : "Elapsed time: 500.563 msecs" | |
; first : "Elapsed time: 500.381 msecs" | |
; seq? : "Elapsed time: 0.291 msecs" | |
; list? : "Elapsed time: 0.286 msecs" | |
; empty? : "Elapsed time: 500.462 msecs" | |
; identity : "Elapsed time: 0.301 msecs" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(benchmark [identity empty? list? seq? first rest next seq] | |
(lazy-seq (cons (slow-computation) | |
(lazy-seq (cons (slow-computation) | |
(lazy-seq (list (slow-computation)))))))) | |
; ======================================================================== | |
(lazy-seq | |
(cons | |
(slow-computation) | |
(lazy-seq | |
(cons (slow-computation) (lazy-seq (list (slow-computation))))))) | |
; seq : "Elapsed time: 500.609 msecs" | |
; next : "Elapsed time: 1000.94 msecs" | |
; rest : "Elapsed time: 500.536 msecs" | |
; first : "Elapsed time: 500.48 msecs" | |
; seq? : "Elapsed time: 0.177 msecs" | |
; list? : "Elapsed time: 0.169 msecs" | |
; empty? : "Elapsed time: 500.566 msecs" | |
; identity : "Elapsed time: 0.154 msecs" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(benchmark [identity empty? list? seq? first rest next seq] | |
(lazy-seq (list (slow-computation) | |
(slow-computation)))) | |
; ======================================================================== | |
(lazy-seq (list (slow-computation) (slow-computation))) | |
; seq : "Elapsed time: 1000.622 msecs" | |
; next : "Elapsed time: 1000.278 msecs" | |
; rest : "Elapsed time: 1000.276 msecs" | |
; first : "Elapsed time: 1000.279 msecs" | |
; seq? : "Elapsed time: 0.474 msecs" | |
; list? : "Elapsed time: 0.157 msecs" | |
; empty? : "Elapsed time: 1000.311 msecs" | |
; identity : "Elapsed time: 0.153 msecs" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defmacro benchmark* | |
[functions lst] | |
(println ";" (str-join "" (repeat 72 \=))) | |
(pprint `~lst) | |
(letfn [(make-fragment [f] `(let [m# ~lst] (~f m#)))] | |
`(do | |
~@(loop [functions functions timers []] | |
(if (seq functions) | |
(recur (rest functions) | |
(cons `(do | |
(print ";" (quote ~(first functions)) ": ") | |
(time ~(make-fragment (first functions))) | |
nil) | |
timers)) | |
timers)) | |
(println)))) | |
(defmacro benchmark** | |
[functions lst] | |
(println ";" (str-join "" (repeat 72 \=))) | |
(pprint `~lst) | |
(letfn [(make-fragment [f] `(let [m# ~lst] (time (~f m#))))] | |
`(do | |
~@(loop [functions functions timers []] | |
(if (seq functions) | |
(recur (rest functions) | |
(cons `(do | |
(print ";" (quote ~(first functions)) ": ") | |
~(make-fragment (first functions)) | |
nil) | |
timers)) | |
timers)) | |
(println)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment