wood burning stoves
The moose likes Java in General and the fly likes How do you evaluate getTime() repeatedly? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How do you evaluate getTime() repeatedly?" Watch "How do you evaluate getTime() repeatedly?" New topic

How do you evaluate getTime() repeatedly?

Art Metzer
Ranch Hand

Joined: Oct 31, 2000
Posts: 241
Hello, everyone.
At http://www.norvig.com/java-iaq.html#math, Peter Norvig claims that counting down (starting at n and decrementing until 0 in a for loop, for example) executes twice as fast as counting up (starting at 0 and incrementing until n). I believe him, but I wanted to see this bit of truth play out for myself.
I then wrote and tried this bit of code:

I got results in the thousands (instead of the millions), and I figure that's because every iteration of my loop, Java incurs the overhead of having to instantiate java.util.Date.
Is there a more efficient way to poll the time every iteration of my for loop? Or, better yet, can you produce source code that would more efficiently/clearly demonstrate Mr. Norvig's statement?
Thank you, one and all.
a.k.a. Curious George
Grant Crofton
Ranch Hand

Joined: Nov 08, 2000
Posts: 154
You can use System.currentTimeMillis() to get a long (I think it's the number of MilliSeconds since 1980, or something).
I tried this:

You'd have to run it a few times to get a fair result. I'll let you see what the answer is.
I agree. Here's the link: http://aspose.com/file-tools
subject: How do you evaluate getTime() repeatedly?
It's not a secret anymore!