This is interesting. We can get quite a huge performance boos by type
hinting that impl.
Times on my system running Clojure 1.4.0:
user=> (defn s-blank? [s] (every? #(Character/isWhitespace %) s))
user=> (time (dotimes [n 2] (s-blank? "asdf")))
"Elapsed time: 247.252 msecs"
Now if we type h
> Given an annotation like this, and assuming that every? is
> clojure.core/every?
>
> (defn blank? [^CharSequence s] (every? #(Character/isWhitespace %) s))
>
> it seems as if it should be possible for the compiler to generate the
> faster code.
>
Yes, there is enough information for a com
Note that the two functions aren't actually equivalent, since the
blank? that uses every? will accept anything that can be made a seq,
while the blank? in clojure.string does not.
Given an annotation like this, and assuming that every? is clojure.core/every?
(defn blank? [^CharSequence s] (every?
On Tuesday, January 15, 2013 9:35:07 PM UTC+1, Thomas wrote:
>
> I think I just answered my own question...
>
> user=> (time (dotimes [n 2] (s-blank? "asdf")))
> "Elapsed time: 2481.018 msecs"
> nil
> user=> (time (dotimes [n 2] (blank? "asdf")))
> "Elapsed time: 14.347 msecs"
> nil
> use
> Quite a difference I have to say.
well, you can still be happy that "first, get it right. then, make it
fast" is still easier in clojure than in java! (of course if, like me,
you are a static typing bigot, there's more to be said on that :-)
--
You received this message because you are subscri
When you find non-idiomatic code in clojure's impl, the reason is almost
always performance.
In this case the clojure.string version is more performant since it saves:
1) the allocation of a lazy seq over the characters of the string
2) the allocation of a java.lang.Character for every char in the
I think I just answered my own question...
user=> (time (dotimes [n 2] (s-blank? "asdf")))
"Elapsed time: 2481.018 msecs"
nil
user=> (time (dotimes [n 2] (blank? "asdf")))
"Elapsed time: 14.347 msecs"
nil
user=>
Quite a difference I have to say.
Thomas
--
You received this message be
Hi all,
In Stuart Halloway's book (Programming Clojure) is a wonderful example of
the succinctness of Clojure where he compares the Apache Commons
implementation of the isBlank method (
http://commons.apache.org/lang/api-2.5/src-html/org/apache/commons/lang/StringUtils.html#line.227)
with a Cl