public
Last active

Shows the impact of using conj with lists in multi-threaded env

  • Download Gist
mt-conj-impact.clj
Clojure
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
;; This gist shows the performance impact of using conj (particularly with lists)
;; in multi-threaded environments
 
(ns thread-test.core
(:require [criterium [core :as c]]))
 
(defn fast-reverse [coll]
(reduce #(cons %2 %1) [] coll))
 
(defn vec-conj [coll]
(reduce conj [] coll))
 
(defn list-conj [coll]
(reduce conj '() coll))
 
(defn list-cons [coll]
(reduce #(cons %2 %1) '() coll))
 
(defn cons-conj [coll]
(reduce conj (clojure.lang.Cons. (first coll) nil) (rest coll)))
 
(defn burn2
([f]
(loop [i 0
value '()]
(if (>= i 10000)
(count (last (take 1000 (iterate f value))))
(recur (inc i)
(cons
(* (int i)
(+ (float i)
(- (int i)
(/ (float i)
(inc (int i))))))
value)))))
([f _] (burn2 f)))
 
 
(defn print-pmap-speedup [label num f]
(println label ": ")
(let [run-test (fn [map-fn]
(-> (c/benchmark
(doall (map-fn f (range 8))) :samples 3) :mean first))
single-thread-time (run-test map)
pmap-time (run-test pmap)]
(println (format "\tmap-ms: %.1f, pmap-ms %.1f, speedup %.02f" single-thread-time pmap-time
(if (zero? pmap-time) 0
(/ (double single-thread-time) pmap-time))))))
 
(defn -main [& args]
(print-pmap-speedup "fast-reverse" 8 (partial burn2 fast-reverse))
(print-pmap-speedup "list-cons" 8 (partial burn2 list-cons))
(print-pmap-speedup "vec-conj" 8 (partial burn2 vec-conj))
(print-pmap-speedup "list-conj" 8 (partial burn2 list-conj))
(print-pmap-speedup "clojure-reverse" 8 (partial burn2 reverse))
(print-pmap-speedup "cons-conj" 8 (partial burn2 cons-conj))
(System/exit 0))

On a dual Xeon E5520 @ 2.27GHz with Linux runnning OpenJDK 1.7.0_09 I get the folliowng output:

fast-reverse : map-ms: 3.3, pmap-ms 0.7, speedup 4.97
list-cons : map-ms: 4.0, pmap-ms 0.7, speedup 6.13
vec-conj : map-ms: 4.0, pmap-ms 1.3, speedup 3.10
list-conj : map-ms: 10.8, pmap-ms 21.2, speedup 0.51
clojure-reverse : map-ms: 13.5, pmap-ms 26.8, speedup 0.50

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.