Last active
August 25, 2016 14:50
-
-
Save moxaj/19fc75f799cde31a27516ef9de4533ef to your computer and use it in GitHub Desktop.
A fast, non-lazy 'repeatedly' implementation
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
(require '[criterium.core :as c]) | |
(set! *warn-on-reflection* true) | |
(set! *unchecked-math* :warn-on-boxed) | |
(defn repeatedly! | |
([^long n f] | |
(loop [i 0 | |
v (transient [])] | |
(if (== i n) | |
(persistent! v) | |
(recur (inc i) (conj! v (f)))))) | |
([^long n kf vf] | |
(loop [i 0 | |
m (transient {})] | |
(if (== i n) | |
(persistent! m) | |
(recur (inc i) (assoc! m (kf) (vf))))))) | |
;; 1.13 us | |
(c/with-progress-reporting | |
(c/quick-bench | |
(repeatedly! 80 #(+ 1 1)))) | |
;; 8.17 us | |
(c/with-progress-reporting | |
(c/quick-bench | |
(doall (repeatedly 80 #(+ 1 1))))) | |
;; 650 ns | |
(c/with-progress-reporting | |
(c/quick-bench | |
(repeatedly! 80 #(+ 1 1) #(+ 1 1)))) | |
;; 13.68 us | |
(c/with-progress-reporting | |
(c/quick-bench | |
(->> (repeatedly 80 (fn [] [2 2])) | |
(into {})))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment