File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do you evaluate getTime() repeatedly?

 
Art Metzer
Ranch Hand
Posts: 241
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Grant Crofton
Ranch Hand
Posts: 154
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic