> I've been optimising a piece of code lately, and have come to wonder about > the performance of keyword comparison. Specifically, I'm not sure whether the > performance I'm seeing is what is expected. The data structures page on > clojure.org [1] indicates that keywords "provide very fast equality tests". > If I micro-benchmark with criterium, then I find the following: > > As a baseline, comparing integers with `(= 0 1)` takes around 4ns. > > Comparing keywords with `(= :plus :minus)` takes around 30ns.
I ran the same test and saw similar results. I noticed that using "identical?" instead of "=" brings the performance much closer to the integer case because "identical?" does less work. In most cases, equal keywords are also identical because the keyword objects are cached by clojure.lang.Keyword/intern. If you don't need to worry about the case of multiple Clojure runtimes being loaded by separate classloaders[1], and if you're in control of the code doing the comparison, you could change to using "identical?" for better performance. Treatment of keyword equality and identity also came up during the development of ClojureScript. I found this discussion interesting: https://groups.google.com/forum/#!topic/clojurescript/bSFK6CEE3PE . --Steve [1] https://groups.google.com/d/msg/clojure/ZW2udohC1lA/6wLcEpQj_LoJ -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.