Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Shows the impact of using conj with lists in multi-threaded env
;; 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
(loop [i 0
value '()]
(if (>= i 10000)
(count (last (take 1000 (iterate f value))))
(recur (inc i)
(* (int i)
(+ (float i)
(- (int i)
(/ (float i)
(inc (int i))))))
([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))

cdorrat commented Dec 9, 2012

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment