Last active
August 26, 2021 17:04
-
-
Save petrounias/9be79d3fcbf6af4c180601048f090747 to your computer and use it in GitHub Desktop.
Clojure concat performance comparison
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
; Comparing 'normal' versus lazy concat | |
(apply concat [[:a] [:b] [:c :d] [:e :f :g] [:h]]) | |
; => (:a :b :c :d :e :f :g :h) | |
(take 3 (apply concat [[:a] [:b] [:c :d] [:e :f :g] [:h]])) | |
; => (:a :b :c) | |
; normal concat | |
(take 3 (apply concat [[:a] [:b] [:c :d] [:e :f :g] [:h]])) | |
; => (:a :b :c) | |
(crit/quick-bench (take 3 (apply concat [[:a] [:b] [:c :d] [:e :f :g] [:h]]))) | |
; Evaluation count : 6014490 in 6 samples of 1002415 calls. | |
; Execution time mean : 107,498409 ns | |
; Execution time std-deviation : 18,745439 ns | |
; Execution time lower quantile : 97,841913 ns ( 2,5%) | |
; Execution time upper quantile : 139,617924 ns (97,5%) | |
; Overhead used : 1,593551 ns | |
; lazy-concat | |
(defn lazy-concat | |
"Lazy version of `(apply concat s)`." | |
[s] | |
(lazy-cat (first s) (when-let [n (next s)] (lazy-concat n)))) | |
(take 3 (lazy-concat [[:a] [:b] [:c :d] [:e :f :g] [:h]])) | |
; => (:a :b :c) | |
(crit/quick-bench (take 3 (lazy-concat [[:a] [:b] [:c :d] [:e :f :g] [:h]]))) | |
; Evaluation count : 21641268 in 6 samples of 3606878 calls. | |
; Execution time mean : 27,053287 ns | |
; Execution time std-deviation : 0,943091 ns | |
; Execution time lower quantile : 25,955953 ns ( 2,5%) | |
; Execution time upper quantile : 28,243797 ns (97,5%) | |
; Overhead used : 1,593551 ns | |
;; large lists | |
; normal concat | |
(first (apply concat [(sort > (range 10)) (sort > (range 1e7))])) | |
; => 9 | |
(crit/quick-bench (first (apply concat [(sort > (range 10)) (sort > (range 1e7))]))) | |
; Evaluation count : 6 in 6 samples of 1 calls. | |
; Execution time mean : 1,099606 sec | |
; Execution time std-deviation : 333,110102 ms | |
; Execution time lower quantile : 767,567999 ms ( 2,5%) | |
; Execution time upper quantile : 1,623283 sec (97,5%) | |
; Overhead used : 1,593551 ns | |
; lazy-concat | |
(first (lazy-concat [(sort > (range 10)) (sort > (range 1e7))])) | |
; => 9 | |
(crit/quick-bench (first (lazy-concat [(sort > (range 10)) (sort > (range 1e7))]))) | |
; Evaluation count : 6 in 6 samples of 1 calls. | |
; Execution time mean : 999,570032 ms | |
; Execution time std-deviation : 240,456317 ms | |
; Execution time lower quantile : 788,094099 ms ( 2,5%) | |
; Execution time upper quantile : 1,268182 sec (97,5%) | |
; Overhead used : 1,593551 ns | |
;; large number of lists | |
; normal concat | |
(first (apply concat (map (fn [_] (repeat 1024 :x)) (range 1e7)))) | |
; => :x | |
(crit/quick-bench (first (apply concat (map (fn [_] (repeat 1024 :x)) (range 1e7))))) | |
; Evaluation count : 719862 in 6 samples of 119977 calls. | |
; Execution time mean : 836,555709 ns | |
; Execution time std-deviation : 12,295324 ns | |
; Execution time lower quantile : 821,947632 ns ( 2,5%) | |
; Execution time upper quantile : 850,561449 ns (97,5%) | |
; Overhead used : 1,593551 ns | |
; lazy-concat | |
(first (lazy-concat (map (fn [_] (repeat 1024 :x)) (range 1e7)))) | |
; => :x | |
(crit/quick-bench (first (lazy-concat (map (fn [_] (repeat 1024 :x)) (range 1e7))))) | |
; Evaluation count : 795798 in 6 samples of 132633 calls. | |
; Execution time mean : 766,662844 ns | |
; Execution time std-deviation : 10,461101 ns | |
; Execution time lower quantile : 759,661193 ns ( 2,5%) | |
; Execution time upper quantile : 784,401171 ns (97,5%) | |
; Overhead used : 1,593551 ns |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment