Skip to content

Instantly share code, notes, and snippets.

@shenfeng
Created December 9, 2011 05:20
Show Gist options
  • Save shenfeng/1450298 to your computer and use it in GitHub Desktop.
Save shenfeng/1450298 to your computer and use it in GitHub Desktop.
(ns just.test
(:require [carbonite.api :as carb])
(:import [java.nio ByteBuffer])
(:import [java.io ObjectOutputStream ByteArrayOutputStream
ByteArrayInputStream ObjectInputStream]))
(defn- serialize [obj]
(let [bao (ByteArrayOutputStream.)
os (ObjectOutputStream. bao)]
(.writeObject os obj)
(.toByteArray bao)))
(defn- deserialize [ba]
(let [oi (ObjectInputStream. (ByteArrayInputStream. ba))]
(.readObject oi)))
(defn prt-str [obj]
(binding [*print-dup* true]
(print-str obj)))
(def registry (carb/default-registry))
(defn carb-serilize [obj]
(let [b (carb/new-buffer 1024)]
(carb/write-buffer registry obj)))
(def obj {:what (range 1 5)
:what2 (map #(str "str-" %) (range 1 5))
:what3 1
:what4 "hello world"})
(defn test-serialize []
;; "Elapsed time: 157.51851 msecs" JDK serialize
(time (dotimes [i 10000]
(serialize obj))) ; size 1110 bytes
;; "Elapsed time: 239.046026 msecs"
(time (dotimes [i 10000] ; clojure print-str
(prt-str obj))) ; size 134 bytes
;; "Elapsed time: 124.11845 msecs"
(time (dotimes [i 10000] ; carbonite
(carb-serilize obj)))) ; size 88 bytes
(defn test-deserialize []
;; "Elapsed time: 915.113472 msecs" JDK
(let [s (serialize obj)]
(time (dotimes [i 10000]
(deserialize s))))
;; "Elapsed time: 218.210735 msecs" clojure
(let [str-obj (prt-str obj)]
(time (dotimes [i 10000]
(read-string str-obj))))
;; "Elapsed time: 121.598514 msecs" carbonite
(let [bs (carb-serilize obj)]
(time (dotimes [i 10000]
(carb/read-buffer registry (ByteBuffer/wrap bs))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment