• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

How do you evaluate getTime() repeatedly?

 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
a wee bit from the empire
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic