Created
December 9, 2012 18:07
-
-
Save llasram/4246320 to your computer and use it in GitHub Desktop.
bench/core.clj
This file contains hidden or 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
(ns bench.core | |
(:require [criterium [core :as c]])) | |
(def | |
^{:arglists '([coll x] [coll x & xs]) | |
:doc "conj[oin]. Returns a new collection with the xs | |
'added'. (conj nil item) returns (item). The 'addition' may | |
happen at different 'places' depending on the concrete type." | |
:added "1.0" | |
:static true} | |
conj* (fn ^:static conj* | |
([coll x] (. clojure.lang.RT (conj coll x))) | |
([coll x & xs] | |
(if xs | |
(recur (conj coll x) (first xs) (next xs)) | |
(conj coll x))))) | |
(defn cons-conj* [coll] | |
(reduce conj* (clojure.lang.Cons. (first coll) nil) (rest coll))) | |
(defn list-conj* [coll] | |
(reduce conj* '() (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 "cons-conj*" 8 (partial burn2 cons-conj*)) | |
(print-pmap-speedup "list-conj*" 8 (partial burn2 list-conj*)) | |
(print-pmap-speedup "cons-conj*" 8 (partial burn2 cons-conj*)) | |
(System/exit 0)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment