Skip to content

Instantly share code, notes, and snippets.

@jjttjj
Last active June 26, 2018 22:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jjttjj/b9ab914009e360f7f765782ef7462bb1 to your computer and use it in GitHub Desktop.
Save jjttjj/b9ab914009e360f7f765782ef7462bb1 to your computer and use it in GitHub Desktop.
Benchmarking [1 2]
hoplon 7.2 x 440,307 ops/sec ±3.15% (58 runs sampled)
array x 728,639 ops/sec ±3.30% (59 runs sampled)
transient x 565,336 ops/sec ±3.71% (56 runs sampled)
non-transient x 688,109 ops/sec ±4.14% (57 runs sampled)
Fastest is  #js [array]
=====================================================
Benchmarking [1 2 3 4 5]
hoplon 7.2 x 217,633 ops/sec ±3.22% (59 runs sampled)
array x 300,999 ops/sec ±3.79% (57 runs sampled)
transient x 273,379 ops/sec ±3.59% (59 runs sampled)
non-transient x 291,401 ops/sec ±3.60% (60 runs sampled)
Fastest is  #js [array]
=====================================================
Benchmarking [1 2 [3 4] 5]
hoplon 7.2 x 188,194 ops/sec ±6.43% (58 runs sampled)
array x 248,692 ops/sec ±8.50% (53 runs sampled)
transient x 213,178 ops/sec ±11.01% (32 runs sampled)
non-transient x 222,303 ops/sec ±9.73% (52 runs sampled)
Fastest is  #js [array]
=====================================================
Benchmarking [1 [2 3 4] [5 6 7] [8 [9 10]]]
hoplon 7.2 x 88,361 ops/sec ±5.45% (55 runs sampled)
array x 128,329 ops/sec ±6.08% (52 runs sampled)
transient x 124,906 ops/sec ±6.58% (52 runs sampled)
non-transient x 127,921 ops/sec ±6.94% (56 runs sampled)
Fastest is  #js [array non-transient transient]
=====================================================
Benchmarking [1 [2] [3] [4 [5 6 7] [8]] 9 10]
hoplon 7.2 x 88,371 ops/sec ±4.61% (58 runs sampled)
array x 138,854 ops/sec ±6.25% (58 runs sampled)
transient x 133,984 ops/sec ±5.63% (55 runs sampled)
non-transient x 132,209 ops/sec ±5.00% (57 runs sampled)
Fastest is  #js [array]
=====================================================
Benchmarking [[[[1]]] [[[[[[2]]] [[[] 3 [[[]]] 4]]]]]]
hoplon 7.2 x 109,258 ops/sec ±6.75% (56 runs sampled)
array x 218,451 ops/sec ±5.44% (46 runs sampled)
transient x 208,861 ops/sec ±5.44% (57 runs sampled)
non-transient x 217,116 ops/sec ±5.47% (58 runs sampled)
Fastest is  #js [array non-transient]
=====================================================
Benchmarking [6 1 3 1 2 7 3 1 1 1 5 2 2 2 7 [9 6 8 9 0 3 4 3 7 0] [0 0 1 5 1 [4 4 1 0 2 3 9 1 7 1] 6 7 9 1] 6 3 [8 5 8 8 8 8 9 1 9 9] 8 8 5 1 [6 3 4 0 1 [2 [6 6 2 5 9 9 [8 2 1 9 9 5 9 7 [1 1 9 5 8 3 9 8 [1 3 9 2 5 8 7 3 5 5] 4] 6] 5 3 8] [4 7 9 6 3 1 6 9 9 5] 7 5 5 7 0 0 5] [9 5 3 4 8 1 8 0 0 [4 5 8 3 3 5 [5 9 [1 5 9 5 8 9 3 8 7 3] [2 7 6 2 6 1 5 2 [8 7 3 [9 4 0 7 1 3 4 7 1 0] [0 2 6 0 3 7 2 1 7 4] 7 1 2 7 4] [6 5 [5 4 9 [3 3 8 0 5 0 3 4 0 4] 1 6 2 0 4 1] 7 6 8 6 3 3 [2 8 2 4 7 5 2 6 3 5]]] 8 1 8 5 3 7] 7 7 1]] 8 1 0] 8 3 8 2 3 8 6 0 6 8 8 3 3 5 2 8 3 0 3 1 9 1 4 3 0 7 0 3 7 6 6 1 6 2 7 4 6 6 [[[4 [5 1 9 8 2 4 0 6 [3 9 5 9 4 6 0 1 8 8] 7] 4 5 3 3 6 1 7 9] 7 3 5 3 3 0 8 [4 8 8 [3 5 5 8 1 9 0 6 5 7] 5 2 2 4 4 3] 0] 2 0 8 3 5 4 2 5 8] 7 2 6 9 7 5 8 [6 8 7 4 4 2 4 8 5 6] 6 1 2 [2 8 4 [4 2 3 8 4 9 0 4 7 9] 1 9 0 0 8 7] 4 [4 1 6 0 0 9 4 0 1 9] 4 9 2 5 6 2 9 2 7 8 6 9 8 0 8 6 8 3 0 1 2 5]
hoplon 7.2 x 2,808 ops/sec ±4.61% (19 runs sampled)
array x 3,705 ops/sec ±7.21% (23 runs sampled)
transient x 3,977 ops/sec ±5.24% (24 runs sampled)
non-transient x 3,286 ops/sec ±5.36% (21 runs sampled)
Fastest is  #js [transient]
=====================================================
(ns shadow-hoplon.vflatten
(:require [hoplon.core :as h]
[javelin.core :as j :refer [defc defc= cell cell=]]
[hoplon.jquery]
[cljs.test :refer-macros [deftest is]]
["benchmark" :as bench]))
(set! (.-Benchmark js/window) bench)
(defn gen-nested-vec [n]
(loop [n n
coll []]
(cond
(zero? n) coll
(< (rand) 0.9) (recur (dec n) (conj coll (rand-int 10)))
:else (recur (dec n) (conj coll (gen-nested-vec 5))))))
;;from hoplon 7.2
(defn- vflattenV0
([tree]
(persistent! (vflattenV0 tree (transient []))))
([tree ret]
(let [l (count tree)]
(loop [i 0]
(if (= i l) ret
(let [x (nth tree i)]
(if-not (sequential? x)
(conj! ret x)
(vflattenV0 x ret))
(recur (inc i))))))))
;;current merge-kids branch
(defn- vflattenV1
([tree]
(persistent! (vflattenV1 tree (transient []))))
([tree ret]
(loop [[x & rst] tree]
(if (sequential? x)
(when (seq x) (vflattenV1 x ret)) ;;edit: remove empty lists
(conj! ret x))
(if-not rst ret (recur rst)))))
(defn- vflattenV2T
"Takes a sequential collection and returns a flattened vector of any nested
sequential collections."
([tree]
(persistent! (vflattenV2T (transient []) tree)))
([acc tree]
(if (sequential? tree)
(reduce vflattenV2T acc tree)
(conj! acc tree))))
(defn- vflattenV2
"Takes a sequential collection and returns a flattened vector of any nested
sequential collections."
([x] (vflattenV2 [] x))
([acc x] (if (sequential? x) (reduce vflattenV2 acc x) (conj acc x))))
(def big-nested-vec [6 1 3 1 2 7 3 1 1 1 5 2 2 2 7 [9 6 8 9 0 3 4 3 7 0] [0 0 1 5 1 [4 4 1 0 2 3 9 1 7 1] 6 7 9 1] 6 3 [8 5 8 8 8 8 9 1 9 9] 8 8 5 1 [6 3 4 0 1 [2 [6 6 2 5 9 9 [8 2 1 9 9 5 9 7 [1 1 9 5 8 3 9 8 [1 3 9 2 5 8 7 3 5 5] 4] 6] 5 3 8] [4 7 9 6 3 1 6 9 9 5] 7 5 5 7 0 0 5] [9 5 3 4 8 1 8 0 0 [4 5 8 3 3 5 [5 9 [1 5 9 5 8 9 3 8 7 3] [2 7 6 2 6 1 5 2 [8 7 3 [9 4 0 7 1 3 4 7 1 0] [0 2 6 0 3 7 2 1 7 4] 7 1 2 7 4] [6 5 [5 4 9 [3 3 8 0 5 0 3 4 0 4] 1 6 2 0 4 1] 7 6 8 6 3 3 [2 8 2 4 7 5 2 6 3 5]]] 8 1 8 5 3 7] 7 7 1]] 8 1 0] 8 3 8 2 3 8 6 0 6 8 8 3 3 5 2 8 3 0 3 1 9 1 4 3 0 7 0 3 7 6 6 1 6 2 7 4 6 6 [[[4 [5 1 9 8 2 4 0 6 [3 9 5 9 4 6 0 1 8 8] 7] 4 5 3 3 6 1 7 9] 7 3 5 3 3 0 8 [4 8 8 [3 5 5 8 1 9 0 6 5 7] 5 2 2 4 4 3] 0] 2 0 8 3 5 4 2 5 8] 7 2 6 9 7 5 8 [6 8 7 4 4 2 4 8 5 6] 6 1 2 [2 8 4 [4 2 3 8 4 9 0 4 7 9] 1 9 0 0 8 7] 4 [4 1 6 0 0 9 4 0 1 9] 4 9 2 5 6 2 9 2 7 8 6 9 8 0 8 6 8 3 0 1 2 5])
(defn- vflattenV3
"Takes a sequential collection and returns a flattened vector of any nested
sequential collections."
([x] (vflattenV3 (array) x))
([acc x]
(if (sequential? x) (reduce vflattenV3 acc x) (doto acc (.push x)))))
;;https://clojureverse.org/t/good-cljs-library-for-benchmarking/1242/5
(defn bench-vflatten []
(let [a [1 2]
b [1 2 3 4 5]
;; c [1 2 [3 4] 5]
;; d [1 [2 3 4] [5 6 7] [8 [9 10]]]
;; e [1 [2] [3] [4 [5 6 7] [8]] 9 10]
;; f [[[[1]]] [[[[[[2]]] [[[] 3 [[[]]] 4]]]]]]
;;g big-nested-vec
]
(doseq [x [a b ;;c d e f g
]]
(println "Benchmarking" x)
(is (= (vflattenV2 x) (vec (vflattenV3 x))))
(doto (bench/Suite.)
(.add "hoplon 7.2" (fn [] (vflattenV0 x)))
(.add "array" (fn [] (vflattenV3 x)))
(.add "transient" (fn [] (vflattenV2T x)))
(.add "non-transient" (fn [] (vflattenV2 x)))
(.on "cycle" (fn [event] (println (str (.-target event)))))
(.on "cycle" (fn [event] (js/console.log event)))
(.on "complete"
(fn []
(this-as this
(println "Fastest is " (.. this (filter "fastest") (map "name"))))))
(.run #js {:async false}))
(println "====================================================="))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment