Skip to content

Instantly share code, notes, and snippets.

@zonpantli
Created February 20, 2011 17:35
Show Gist options
  • Save zonpantli/836135 to your computer and use it in GitHub Desktop.
Save zonpantli/836135 to your computer and use it in GitHub Desktop.
generate a-count random ints with Java Random
(use 'criterium.core)
(defn randoms [seed a-count]
(let [r (Random. seed)]
(loop [vec []
cnt a-count]
(if (zero? cnt)
vec
(recur (conj vec (.nextInt r)) (dec cnt))))))
(with-progress-reporting (bench (randoms 1 10000)))
;; Execution time mean : 2.236724 ms 95.0% CI: (2.236528 ms, 2.236869 ms)
;; Execution time std-deviation : 499.451702 us 95.0% CI: (493.492747 us, 504.166701 us)
(defn randoms-2 [seed a-count]
(let [r (Random. seed)]
(vec (take a-count (map #(.nextInt %) (repeat r))))))
(with-progress-reporting (bench (randoms-2 1 10000)))
;; Execution time mean : 44.779167 ms 95.0% CI: (44.772901 ms, 44.784634 ms)
;; Execution time std-deviation : 3.207835 ms 95.0% CI: (3.192019 ms, 3.234287 ms)
(defn randoms-2-no-reflection [seed a-count]
(let [r (Random. seed)]
(vec (take a-count (map #(.nextInt ^Random %) (repeat r))))))
(with-progress-reporting (bench (randoms-2-no-reflection 1 10000)))
;; Execution time mean : 7.914738 ms 95.0% CI: (7.914161 ms, 7.915397 ms)
;; Execution time std-deviation : 646.576370 us 95.0% CI: (643.824827 us, 649.333655 us)
(defn randoms-3 [seed a-count]
(let [r (Random. seed)]
(vec (for [i (range a-count)]
(.nextInt r)))))
(with-progress-reporting (bench (randoms-3 1 10000)))
;; Execution time mean : 1.992743 ms 95.0% CI: (1.991795 ms, 1.993827 ms)
;; Execution time std-deviation : 2.629739 ms 95.0% CI: (2.618777 ms, 2.654392 ms)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment