_评论由:gshayban_ 提供
重放代码
(def C (atom 0))
;;重新扩展以进行仪器检查
(extend-protocol clojure.core.protocols/IKVReduce
;;慢路径默认
clojure.lang.IPersistentMap
(kv-reduce
[amap f init]
(swap! C inc)
(reduce (fn [ret [k v]] (f ret k v)) init amap)))
(reduce-kv (fn [_ _ _]) nil (array-map 1 2 3 4))
(println "slowpaths" @C)
(reduce-kv (fn [_ _ _]) nil (hash-map 1 2 3 4))
(println "slowpaths" @C)
重复输出
➜ dev /usr/lib/jvm/java-8-openjdk/bin/java -cp $(clojure -Spath) clojure.main kvfastpath.clj
slowpaths 1
slowpaths 2
➜ dev /usr/lib/jvm/java-11-openjdk/bin/java -cp $(clojure -Spath) clojure.main kvfastpath.clj
slowpaths 0
slowpaths 0