Just done some of my own quick benchmarking on this.

(= :foo :bar)          ;; 21.7 ns
(.equals :foo :bar)    ;;  3.0 ns
(identical? :foo :bar) ;;  3.0 ns

(.equals "foo" "bar")  ;;  5.6 ns
(= "foo" "bar")        ;; 24.4 ns

This seems to support my suspicion that the overhead is in the extra 
overhead in clojure.core/=, rather than an intrinsic issue with keyword 
equality performance.

On Saturday, 11 October 2014 18:41:12 UTC+8, Mikera wrote:
>
> I believe this is a symptom of the fact that the Clojure compiler isn't 
> very type aware, and = inserts a bunch of redundant runtime type checks 
> (see clojure.lang.Util.equiv(Object, Object) )
>
> In particular, it always checks for whether the arguments are instances of 
> Number or IPersistentCollection first, because these get special handling, 
> before calling Keyword.equals(). Obviously, these checks are pointless if 
> you already know one or both arguments is a keyword, but the compiler 
> doesn't make that inference at compile time so we have to pay the runtime 
> overhead. These type checks are pretty cheap, but certainly not free. It's 
> also possible that the JVM isn't inlining as much as it could.
>
> On Saturday, 11 October 2014 06:23:10 UTC+8, Jony Hudson wrote:
>>
>> Hi All,
>>
>>  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.
>>
>> This is about the same amount of time it takes to compare strings, `(= 
>> "plus" "minus")`, which comes in at about 25ns.
>>
>> This surprised me, as I would have guessed that "fast" would have been 
>> closer to the integer performance than the string performance. It's worth 
>> saying that I don't know a lot about benchmarking, but I do have some 
>> "real" code that's performance depends heavily on comparisons, and it seems 
>> to line up performance-wise with these micro-benchmarks.
>>
>> So, am I doing something silly (like I don't know about the fast = for 
>> keywords)? Or, are my expectations wrong, and this is about how long "fast" 
>> should be? Or is there a performance bug lurking?
>>
>> I'm using Clojure 1.6.0 (but have tried 1.5.0 and 1.7.0-alpha1 with 
>> similar results).
>> x86_64 Mac OS X 10.9.5 4 cpu(s)
>>
>> Java HotSpot(TM) 64-Bit Server VM 25.5-b02
>>
>> Thanks in advance for any input,
>>
>>
>> Jony
>>
>> [1] http://clojure.org/data%5Fstructures
>>
>

-- 
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.

Reply via email to