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. Art a.k.a. Curious George
posted 15 years ago
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.