> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@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+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
--
Sent from an IBM Model M, 15 August 1989.
(defn even?
"Returns true if n is even, throws an exception if n is not an integer"
{:added "1.0"
:static true}
[n] (zero? (bit-and (long n) (long 1))))
before your example.
It is fast on my computer.
(I believe there is a reflective call, without the explicit cast.)
David, Nicolas, thank you for finding the culprit so quickly :)
What profiling technique/tool did you use? I have some other code
that is also much slower in 1.3, and thought I'd take a crack at
finding the culprit myself before spamming the list again.
Yes, max-key calls f more times than necessary.
http://code.google.com/p/clojure/issues/detail?id=95
We ran into this problem yesterday on a tic-tac-toe project, in which
f was a function that determines who is winning for a given board
position. Tests that previously ran in under a minute now took over 10
minutes (that's roughly how long I waited before stopping it). We
fixed our program by replacing max-key with the alternative
implementation from the above link.
http://github.com/ericlavigne/tic-tac-toe/commit/0d172c45c9d462620af8b819db53e19ab6382bf6
I use that too.
Sampling for a first look, profiling with instrumentation for a more
precise answer.
(Here, the sampling gives even? and the profiling gives the reflection methods)
I just looked over this code. You can speed it up even more by
manually encoding the loop, rather than using reduce.
(defn faster-max-key
([k x] x)
([k x & more]
(loop [x x,
kx (k x)
s more]
(if-not s x
(let [y (first s),
ky (k y)]
(if (> kx ky)
(recur x kx (next s))
(recur y ky (next s))))))))
5x faster than the one at the code.google.com link above in my own benchmarks.
Actually, I'd expect that expt would no longer be guaranteed to
produce the right results for large integers in 1.3.
The whole point of clojure.contrib.math is to extend math operations
to the full numeric tower supported by Clojure and 1.3 breaks that.
I haven't tried 1.3 yet, but I'd recommend downloading a copy of
clojure.contrib.math locally and replace any instances of +, -, *,
inc, dec with +', -', *', inc', dec'. This should at least make the
functions produce the correct results. I'd be curious to know whether
performance continues to be bad after making those changes, so if you
do this experiment, please report your results.
--Mark
I'm still stumped as to why expt and sqrt would be 100x slower. My
first thought is that the loop/recur machinery has changed in 1.3, to
support primitives in the recur, so perhaps there's some extra back
and forth boxing/unboxing going on, or perhaps loop/recur is just
fundamentally slower now? Another possibility is that all the literal
numbers are now longs instead of Integers, so maybe that's slowing
down the computations?
I'd be curious to know whether explicitly boxing everything in the
second line of expt-int helps the performance at all (along with the '
math operators), i.e.,
(defn- expt-int [base pow]
(loop [n pow, y (num 1), z base]
to
(defn- expt-int [base pow]
(loop [n (num pow), y (num 1), z (num base)]
Wouldn't it be more consistent if bit-shift-left provided an overflow
error for long inputs that shift so much they overflow? Should there
also be an unchecked-bit-shift-left and a bit-shift-left' ?