I didn't know reduce could be short circuited! I assume from the code below 
this is done calling the function reduced? Is this in Clojure 1.5 only and 
is where is this documented?

On Wednesday, February 27, 2013 4:59:33 AM UTC-5, Christophe Grand wrote:
>
>
> On Wed, Feb 27, 2013 at 10:21 AM, Marko Topolnik 
> <marko.t...@gmail.com<javascript:>
> > wrote:
>
>>
>> (defn blank? [s] (every? #(Character/isWhitespace %) s))
>>>
>>> Have you ever wondered about its performance? Here you go:
>>>
>>> user> (time (dotimes [_ 10000] (blank? "                                 
>>>             ")))
>>> "Elapsed time: 3887.578 msecs"
>>>
>>
>> To give a more complete picture, this version
>>
>> (defn blank? [s] (every? #(Character/isWhitespace ^char %) s)) 
>>
>> is only six times slower than the expanded version, and keeping an eye on 
>> reflection warnings is not such a drag. So, if it could be demonstrated 
>> that in general the properly type-hinted, but otherwise idiomatic Clojure 
>> is not more than 10 times slower than idiomatic Java, I'd consider that at 
>> least a good starting point.
>>
>
> Now that reduce can be short-circuited, redifining every?, some and al on 
> top of it would yield some interesting gains:
>
> (defn revery? [pred coll]
>   (reduce (fn [t x]
>             (if (pred x)
>               t
>               (reduced false))) true coll))
>
> (defn rblank? [s] (revery? #(Character/isWhitespace ^char %) s))
> (defn blank? [s] (every? #(Character/isWhitespace ^char %) s)) 
>
> => (dotimes [_ 10]
>      (time (dotimes [_ 100000] (blank? "                                   
>           "))))
> "Elapsed time: 515.371 msecs"
> "Elapsed time: 500.408 msecs"
> "Elapsed time: 507.646 msecs"
> "Elapsed time: 644.074 msecs"
> "Elapsed time: 529.717 msecs"
> "Elapsed time: 482.813 msecs"
> "Elapsed time: 557.563 msecs"
> "Elapsed time: 486.573 msecs"
> "Elapsed time: 493.636 msecs"
> "Elapsed time: 481.357 msecs"
> nil
> => (dotimes [_ 10]
>      (time (dotimes [_ 100000] (rblank? "                                 
>             "))))
> "Elapsed time: 227.692 msecs"
> "Elapsed time: 99.937 msecs"
> "Elapsed time: 95.922 msecs"
> "Elapsed time: 91.193 msecs"
> "Elapsed time: 90.794 msecs"
> "Elapsed time: 94.765 msecs"
> "Elapsed time: 89.842 msecs"
> "Elapsed time: 120.551 msecs"
> "Elapsed time: 90.843 msecs"
> "Elapsed time: 93.523 msecs"
> nil
>
> Christophe
>  

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