On Tue, May 3, 2011 at 8:03 AM, David Nolen <[email protected]> wrote:
> Why not?
> user> (time (dotimes [_ 1000000000] (Math/ceil 0.1)))
> "Elapsed time: 626.867 msecs"
> David
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).
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en