Last active
December 17, 2015 04:33
-
-
Save camsaul/b8633f71941a0198f713 to your computer and use it in GitHub Desktop.
function that returns function to compare performance of 2 different implementations
This file contains hidden or 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
(defn implementation-comparitor [f1 f2] | |
(let [f1-runtime (atom 0) | |
f2-runtime (atom 0) | |
performance-ratios (atom [])] | |
(fn [& args] | |
(let [f1-start (System/nanoTime) | |
_ (apply f1 args) | |
f1-time (- (System/nanoTime) f1-start) | |
f2-start (System/nanoTime) | |
return (apply f2 args) | |
f2-time (- (System/nanoTime) f2-start) | |
this-ratio (/ (double f1-time) f2-time)] | |
(swap! f1-runtime + f1-time) | |
(swap! f2-runtime + f2-time) | |
(swap! performance-ratios conj this-ratio) | |
(println (u/format-color (cond (< this-ratio 0.66) 'red | |
(> this-ratio 1.50) 'green | |
:else 'cyan) | |
"f1 time: %.1fµs f2 time: %.1fµs performance ratio this round (%d rounds): %.2fx avg: %.2fx median: %.2fx" | |
(/ f1-time 1000.0) | |
(/ f2-time 1000.0) | |
(count @performance-ratios) | |
this-ratio | |
(/ (double @f1-runtime) @f2-runtime) | |
(nth (sort @performance-ratios) | |
(int (/ (count @performance-ratios) 2))))) | |
return)))) |
Author
camsaul
commented
Dec 17, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment