Skip to content

Instantly share code, notes, and snippets.

@timmc
Created March 23, 2012 15:04
Show Gist options
  • Select an option

  • Save timmc/2171507 to your computer and use it in GitHub Desktop.

Select an option

Save timmc/2171507 to your computer and use it in GitHub Desktop.
Profiling ints vs floats
(ns clj.core
(:use [criterium.core :only (bench with-progress-reporting)]))
(defmacro accumulate
"Captures symbol 'acc as accumulator."
[init-e next-e]
`(loop [c# (int 1e8)
~'acc ~init-e]
(if (zero? c#)
~'acc
(recur (dec c#) ~next-e))))
(defn int-add
[]
(let [addend (int 3)]
(accumulate (int 0) (+ acc addend))))
(defn float-mult
[]
(let [multiplier (float 1.1)]
(accumulate (float 1.1) (* acc multiplier))))
(defn float-mult-rand
[]
(let [multiplier (float (* 0.1 (rand)))]
(accumulate (float 1.1) (* acc multiplier))))
(defn Double-mult
[]
(let [multiplier (Double. 1.1)]
(accumulate (Double. 1.1) (* acc multiplier))))
(defn -main [& args]
(with-progress-reporting (bench (int-add)))
(with-progress-reporting (bench (float-mult)))
(with-progress-reporting (bench (float-mult-rand))) ;; verifies that mult by float literal wasn't being optimized
(with-progress-reporting (bench (Double-mult)))) ;; compare with boxed Double
Execution time mean : 109.997211 ms 95.0% CI: (109.995849 ms, 109.998204 ms)
Execution time std-deviation : 151.345935 us 95.0% CI: (149.084999 us, 154.319081 us)
Execution time lower ci : 109.903555 ms 95.0% CI: (109.903148 ms, 109.903555 ms)
Execution time upper ci : 110.151331 ms 95.0% CI: (110.151331 ms, 110.156871 ms)
Execution time mean : 157.579352 ms 95.0% CI: (157.578481 ms, 157.580445 ms)
Execution time std-deviation : 135.361621 us 95.0% CI: (134.368821 us, 136.967871 us)
Execution time lower ci : 157.474241 ms 95.0% CI: (157.474241 ms, 157.474241 ms)
Execution time upper ci : 157.732024 ms 95.0% CI: (157.721684 ms, 157.732024 ms)
Execution time mean : 157.555778 ms 95.0% CI: (157.554430 ms, 157.557141 ms)
Execution time std-deviation : 167.463668 us 95.0% CI: (167.249062 us, 167.719124 us)
Execution time lower ci : 157.469141 ms 95.0% CI: (157.469141 ms, 157.469723 ms)
Execution time upper ci : 157.616224 ms 95.0% CI: (157.616224 ms, 157.616345 ms)
Execution time mean : 944.139073 ms 95.0% CI: (943.967600 ms, 944.297028 ms)
Execution time std-deviation : 18.603355 ms 95.0% CI: (18.481447 ms, 18.681223 ms)
Execution time lower ci : 920.738442 ms 95.0% CI: (920.738442 ms, 920.738442 ms)
Execution time upper ci : 972.390531 ms 95.0% CI: (971.889535 ms, 972.390531 ms)
(defproject clj "1.0.0-SNAPSHOT"
:dependencies [[org.clojure/clojure "1.3.0"]
[criterium "0.2.1-SNAPSHOT"]]
:main clj.core)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment