Thank you!

Adding type hints solves the problem. The interop now slightly outperforms 
Clojure's get and count.

So how come Clojure's get and count don't incur the reflection penalty?

On Sunday, February 17, 2013 9:17:55 AM UTC-5, Geo wrote:
>
> I am writing an expensive algorithms in Clojure and while trying to 
> optimize the code I discovered something that puzzled me. Clojure count and 
> get functions are much faster on strings than direct interop with .length 
> and .charAt. On my machine I get the following:
>
> (def sss (apply str (repeat 10000000 \a)))
>
> I execute each of the following tests a few times:
>
> (time (dotimes [_ 1000] (count sss)))
> "Elapsed time: 0.56 msecs"
> "Elapsed time: 0.539 msecs"
> "Elapsed time: 0.551 msecs"
> "Elapsed time: 0.453 msecs"
> "Elapsed time: 0.612 msecs"
>
> (time (dotimes [_ 1000] (.length sss)))
> "Elapsed time: 170.819 msecs"
> "Elapsed time: 114.892 msecs"
> "Elapsed time: 10.111 msecs"
> "Elapsed time: 32.106 msecs"
> "Elapsed time: 10.803 msecs"
>
> Even after something under the hood warms up (feel free to enlighten me 
> :). .length is still significantly slower.
>
> (time (dotimes [_ 1000] (get sss (rand-int 1000))))
> "Elapsed time: 4.651 msecs"
> "Elapsed time: 3.699 msecs"
> "Elapsed time: 3.672 msecs"
> "Elapsed time: 4.561 msecs"
> "Elapsed time: 3.742 msecs"
>
> (time (dotimes [_ 1000] (.charAt sss (rand-int 1000))))
> "Elapsed time: 13.211 msecs"
> "Elapsed time: 14.874 msecs"
> "Elapsed time: 32.044 msecs"
> "Elapsed time: 13.849 msecs"
> "Elapsed time: 39.493 msecs"
>
> .charAt is also significantly slower than get.
>
> By replacing .length with count and .charAt with get I was able to reduce 
> the running time of my algo by orders of magnitude.
>
> These results are surprising and puzzling. You'd think direct interop with 
> Java would be the faster. Can anyone venture a guess or explain what's 
> going on here?
>
>

-- 
-- 
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/groups/opt_out.


Reply via email to