Skip to content

Instantly share code, notes, and snippets.

@mmcgrana
Created February 27, 2010 13:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mmcgrana/316675 to your computer and use it in GitHub Desktop.
Save mmcgrana/316675 to your computer and use it in GitHub Desktop.
(require '[clj-serializer.core :as serializer])
(require '[clj-json.core :as m-json])
(require '[org.danlarkin.json :as dl-json])
(require '[clojure.contrib.json.read :as cc-json-read])
(require '[clojure.contrib.json.write :as cc-json-write])
(defn nano-time []
(System/nanoTime))
(defn timed [task]
(let [t-start (nano-time)
res (task)
t-end (nano-time)]
(double (/ (- t-end t-start) 1000000000))))
(def record
{:keyword :foo
:string "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
:integer 3
:long 52001110638799097
:double 1.23
:boolean true
:nil nil
:map {"hi" 9 "low" 0}
:vector ["a" "b" "c"]
:list '(1 2 3)})
(def roundtrips 100000)
(defn bench [name f]
(print name " ")
(flush)
(printf "%.2f\n" (timed #(dotimes [_ roundtrips] (f record))))
(flush))
(println "Clojure version: "
(str (:major *clojure-version*) "."
(:minor *clojure-version*) "."
(:incremental *clojure-version*)))
(println "Num roundtrips: " roundtrips)
(println)
(dotimes [i 2]
(println "Trail: " (inc i))
(bench "clj-json "
#(m-json/parse-string (m-json/generate-string %)))
(bench "clj-json w/ keywords "
#(reduce (fn [m [k v]] (assoc m (keyword k) v)) {}
(m-json/parse-string (m-json/generate-string %))))
(bench "clj-serializer "
#(serializer/deserialize (serializer/serialize %) :eof))
(bench "clojure printer/reader "
#(read-string (prn-str %)))
(bench "clojure printer/reader w/ print-dup "
#(binding [*print-dup* true] (read-string (prn-str %))))
(bench "org.danlarkin.json "
#(dl-json/decode-from-str (dl-json/encode-to-str %)))
(bench "clojure.contrib.json "
#(cc-json-read/read-json (cc-json-write/json-str %)))
(bench "clojure.contrib.json w/ keywords "
#(binding [cc-json-read/*json-keyword-keys* true]
(cc-json-read/read-json (cc-json-write/json-str %))))
(println))
Clojure version: 1.1.0
Num roundtrips: 100000
Trail: 1
clj-json 2.88
clj-json w/ keywords 9.59
clj-serializer 3.35
clojure printer/reader 8.77
clojure printer/reader w/ print-dup 15.59
org.danlarkin.json 41.94
clojure.contrib.json 35.68
clojure.contrib.json w/ keywords 47.19
Trail: 2
clj-json 1.91
clj-json w/ keywords 9.32
clj-serializer 2.93
clojure printer/reader 7.85
clojure printer/reader w/ print-dup 19.76
org.danlarkin.json 41.34
clojure.contrib.json 35.16
clojure.contrib.json w/ keywords 49.18
try on 1.2.0 with new protocol-based pure-Clojure implementations
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment