That's a terrible ceil method: (inc (int 0)) is 1. The reason it's
fast is that (int x) is a java primitive. It acts like the *floor*
function, making it not useful to you.
--
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
Because I did not remember Math/ceil :-).Point is, is there any consensus on what math library to use? Is (Math/... in general the fastest?
It's optimizing your loop away, or else you're using ridiculously
powerful hardware.
user=> (time (dotimes [_ 1000000] (Math/ceil (rand))))
"Elapsed time: 142.86748 msecs"
nil
Making it a local of type "double" seems slightly faster:
user=> (time (dotimes [_ 1000000] (let [x (double (rand))] (Math/ceil x))))
"Elapsed time: 136.96752 msecs"
nil
But to really time a "ceil" function we should have premade random
numbers and avoid including their generation in the time. We'll also
get rid of some boxing and function call overheads:
user=> (let [nums (double-array (repeatedly 1000000 rand))]
(time
(dotimes [i 1000000] (Math/ceil (aget nums i)))))
"Elapsed time: 47.99892 msecs"
nil
Now the only things in the timing loop are: counting to 1 million,
aget from a primitive array, and Math/ceil itself. That's about as
good as it's gonna get.
Substitute calls to other ceil implementations as appropriate.
Oh, and by the way, (int x) is not quite floor either:
user=> (int -2.1)
-2
It truncates towards 0 rather than -infinity.
#(quot % 1) does the same thing (and is slower by nearly a factor of 10).
It's optimizing your loop away, or else you're using ridiculouslypowerful hardware.
user=> (time (dotimes [_ 1000000] (Math/ceil (rand))))
"Elapsed time: 142.86748 msecs"
nil
On Tue, May 3, 2011 at 2:51 PM, Ken Wesson <kwes...@gmail.com> wrote:It's optimizing your loop away, or else you're using ridiculouslypowerful hardware.
user=> (time (dotimes [_ 1000000] (Math/ceil (rand))))
"Elapsed time: 142.86748 msecs"
nilMaybe, maybe not: