Skip to content

Instantly share code, notes, and snippets.

@camsaul
Last active December 17, 2015 04:33
Show Gist options
  • Save camsaul/b8633f71941a0198f713 to your computer and use it in GitHub Desktop.
Save camsaul/b8633f71941a0198f713 to your computer and use it in GitHub Desktop.
function that returns function to compare performance of 2 different implementations
(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))))
@camsaul
Copy link
Author

camsaul commented Dec 17, 2015

screen shot 2015-12-16 at 8 33 22 pm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment