Skip to content

Instantly share code, notes, and snippets.

@serioga
Last active August 30, 2020 10:04
Show Gist options
  • Save serioga/60499dab8248a58899d10a2687d5b77c to your computer and use it in GitHub Desktop.
Save serioga/60499dab8248a58899d10a2687d5b77c to your computer and use it in GitHub Desktop.
(ns edn-benchmark
"For https://twitter.com/GirlGameDev/status/1299153999057375237
https://github.com/naomijub/edn-duration-benchmark"
(:require [clojure.edn :as edn]))
(set! *warn-on-reflection* true)
;;;; tested in REPL with `criterium.core/quick-bench`
;;;; windows 10, CPU 3.30 GHz, RAM 16 GB
;;;; clojure "1.10.1", java 11
(def edn
"{
:type :human
:first-name \"bench\"
:last-name \"mark\"
:age 13
:version 0.13
:associates [
{
:name :julia
:role :adm
}
{
:name :otavio
:role :contributor
}
{
:name :juxt
:role :great-ideas
}
]
}")
(def edn-parsed (edn/read-string edn)) #_{:type :human,
:first-name "bench",
:last-name "mark",
:age 13,
:version 0.13,
:associates [{:name :julia, :role :adm}
{:name :otavio, :role :contributor}
{:name :juxt, :role :great-ideas}]}
(comment
(edn/read-string edn))
;Evaluation count : 25656 in 6 samples of 4276 calls.
; Execution time mean : 24,712855 µs
; Execution time std-deviation : 1,406317 µs
; Execution time lower quantile : 23,336300 µs ( 2,5%)
; Execution time upper quantile : 26,987937 µs (97,5%)
; Overhead used : 9,186638 ns
(comment
(get-in edn-parsed [:associates 0 :role]) #_:adm)
;Evaluation count : 2185362 in 6 samples of 364227 calls.
; Execution time mean : 274,184082 ns
; Execution time std-deviation : 11,240045 ns
; Execution time lower quantile : 264,457822 ns ( 2,5%)
; Execution time upper quantile : 292,044361 ns (97,5%)
; Overhead used : 9,186638 ns
(comment
(-> edn-parsed :associates (get 0) :role) #_:adm)
;Evaluation count : 9417012 in 6 samples of 1569502 calls.
; Execution time mean : 52,177678 ns
; Execution time std-deviation : 16,744658 ns
; Execution time lower quantile : 42,956144 ns ( 2,5%)
; Execution time upper quantile : 80,146508 ns (97,5%)
; Overhead used : 9,186638 ns
(comment
(-> edn-parsed :associates (nth 0) :role) #_:adm)
;Evaluation count : 14529156 in 6 samples of 2421526 calls.
; Execution time mean : 35,197372 ns
; Execution time std-deviation : 0,720250 ns
; Execution time lower quantile : 34,401042 ns ( 2,5%)
; Execution time upper quantile : 36,115705 ns (97,5%)
; Overhead used : 9,186638 ns
;;;
;;; Implementation with records
;;;
(defrecord Associate [name role])
(defrecord Creature [type first-name last-name age version associates])
(def edn-rec
"#edn_benchmark.Creature{:type :human, :first-name \"bench\", :last-name \"mark\", :age 13, :version 0.13, :associates [#edn_benchmark.Associate{:name :julia, :role :adm} #edn_benchmark.Associate{:name :otavio, :role :contributor} #edn_benchmark.Associate{:name :juxt, :role :great-ideas}]}")
(def edn-rec-parsed
(->> edn-rec (edn/read-string {:readers {'edn_benchmark.Creature map->Creature
'edn_benchmark.Associate map->Associate}}))) #_#edn_benchmark.Creature{:type :human,
:first-name "bench",
:last-name "mark",
:age 13,
:version 0.13,
:associates [#edn_benchmark.Associate{:name :julia, :role :adm}
#edn_benchmark.Associate{:name :otavio, :role :contributor}
#edn_benchmark.Associate{:name :juxt, :role :great-ideas}]}
(comment ; Parse EDN
(->> edn-rec (edn/read-string {:readers {'edn_benchmark.Creature map->Creature
'edn_benchmark.Associate map->Associate}})))
;Evaluation count : 16308 in 6 samples of 2718 calls.
; Execution time mean : 35,813287 µs
; Execution time std-deviation : 4,658000 µs
; Execution time lower quantile : 31,724036 µs ( 2,5%)
; Execution time upper quantile : 41,290869 µs (97,5%)
; Overhead used : 9,186638 ns
(comment ; Access as map
(-> edn-rec-parsed :associates (nth 0) :role) #_:adm)
;Evaluation count : 27883824 in 6 samples of 4647304 calls.
; Execution time mean : 12,385923 ns
; Execution time std-deviation : 0,192291 ns
; Execution time lower quantile : 12,227132 ns ( 2,5%)
; Execution time upper quantile : 12,680454 ns (97,5%)
; Overhead used : 9,186638 ns
(comment ; Access record fields directly
(.-role ^Associate (nth (.-associates ^Creature edn-rec-parsed) 0)) #_:adm)
;Evaluation count : 35283432 in 6 samples of 5880572 calls.
; Execution time mean : 7,579602 ns
; Execution time std-deviation : 0,118763 ns
; Execution time lower quantile : 7,453155 ns ( 2,5%)
; Execution time upper quantile : 7,735706 ns (97,5%)
; Overhead used : 9,186638 ns
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment