• 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
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

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.
 
Where does a nanny get ground to air missles? Protect this tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic