Why does java native HashMap in clojure run slowly? -
i associate key hash map 10000000 time. here's java code , output:
import java.util.hashmap; public class testmap { public static void main(string[] args) { hashmap<integer, integer> mp = new hashmap<integer, integer>(); long start = system.currenttimemillis(); (int = 0; < 10000000; i++) { mp.put(1, 1); } long end = system.currenttimemillis(); system.out.println("elapsed time: " + (end - start) + " msecs"); } } $ javac testmap.java && java -cp . testmap elapsed time: 38 msecs
and call java clojure in repl:
user=> (import java.util.hashmap) java.util.hashmap user=> (def mp (hashmap.)) #'user/mp user=> (time (dotimes [n 10000000] (.put mp 1 1))) "elapsed time: 10024.797 msecs" nil
both code same thing, clojure version runs exstreamly slow!!
what's problem?
add type hint better:
user> (import 'java.util.hashmap) java.util.hashmap user> (def mp (hashmap.)) #'user/mp user> (time (dotimes [n 10000000] (.put mp 1 1))) "elapsed time: 13932.248126 msecs" nil user> (time (dotimes [n 10000000] (.put ^hashmap mp 1 1))) "elapsed time: 117.915992 msecs" nil
Comments
Post a Comment