clojure - ClojureScript map lookup slow -
i have simple map:
(def my-map {[1 2 3] 1 [1 2 4] 5 [3 4 2] 3 [4 5 3] 3 [5 2 5] 6 [9 2 1] 5 [8 3 1] 6}) that use performing lookups. performs rather poorly, however:
(time (doseq [x (range 500)] (my-map [1 2 8]))) "elapsed time: 170 msecs" on same machine, clojure can 500,000 in 236 msecs, or 700x faster. while it's not unexpected clojure faster clojurescript, i'm confused why clojurescript slower.
any ideas how make simple multi-valued lookup map above efficiently , in readable manner in clojurescript? know doing bunch of ifs instead of using vector-key solution work faster, i'm looking @ that's more readable / maintainable.
just update more information. above done in firefox, slower compared v8. following:
(def my-map2 (into cljs.core.persistenthashmap/empty {[1 2 3] 1 [1 2 4] 5 [3 4 2] 3 [4 5 3] 3 [5 2 5] 6 [9 2 1] 5 [8 3 1] 6})) (defn p1 [] (let [v [1 2 8]] (dotimes [_ 5] (time (dotimes [_ 500000] (get my-map2 v)))))) gives:
"elapsed time: 3295 msecs" "elapsed time: 3246 msecs" "elapsed time: 3113 msecs" "elapsed time: 3107 msecs" "elapsed time: 3121 msecs" in chromium version 25.0.1364.160 ubuntu 13.04 (25.0.1364.160-0ubuntu3). still 13x times slower in clojurescript clojure, that's better before. note i'm running directly in browser repl.
on machine running exact example advanced compilation takes ~14ms on 1.7ghz macbook air running relatively recent v8 built source.
to make sure we're benchmarking think we're benchmarking it's best write this:
(let [v [1 2 8]] (dotimes [_ 5] (time (dotimes [_ 500000] (get my-map v))))) on machine takes ~70ms on machine clojure jvm. clojurescript runs around ~3600ms, 50x slower. why? it's because default persistentarraymap clojure not when defining small hash maps complex keys.
what happens if define my-map instead:
(def my-map (into cljs.core.persistenthashmap/empty [[1 2 3] 1 [1 2 4] 5 [3 4 2] 3 [4 5 3] 3 [5 2 5] 6 [9 2 1] 5 [8 3 1] 6])) the benchmark takes ~170ms, isn't far off clojure jvm.
so there's plenty of optimizations clojure implements haven't gotten around yet. still i'd idiomatic clojure code think best can hope on highly tuned javascript engines v8 2-10x of clojure jvm..
Comments
Post a Comment