Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Moon Whaler
Jul 1, 2007

I have a question about Java nanoTime. I'm trying to use it to measure the running time of some code and most of the time it returns a value around 130,000, but sometimes it returns values around 3,750,000 or -3,750,000. I'm running a dual core processor on windows and my only theory is that the dual core is messing up the nanoTime call somehow. Any ideas on what is happening/how to fix it?

Adbot
ADBOT LOVES YOU

Moon Whaler
Jul 1, 2007

dancavallaro posted:

nanoTime makes no guarantees about the *accuracy* of the time, only the *precision*. Meaning, you can't use System.nanoTime() to actually get an accurate measure of the current time, but the difference between two calls to nanoTime is the most precise elapsed time measurement possible in Java.

Yeah but telling me my program finishes 3ms before it starts seems a little off

Edit: Yeah, i'm subtracting the time before from the time after, it just sometimes gives negative results

Moon Whaler fucked around with this message at 06:17 on Mar 5, 2009

Moon Whaler
Jul 1, 2007

csammis posted:

Is it possible that the data type you're using to store the nanoTime result (or the subtraction result) is overflowing and wrapping?

I don't think so, I'm using a long. I'm pretty sure what' happening is sometimes it tries to compare times given by two different processors and gets a garbage result. I guess I'll just have to run it on another machine.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply