Results 1 to 4 of 4
  1. #1
    karl_marx is offline Member
    Join Date
    Jun 2013
    Posts
    2
    Rep Power
    0

    Unhappy Problem with System.nanoTime()

    Hello i am following a tutorial and im making a timer following this. However i seem to have a problem with System.nanoTime. It seems that it only updates its value every microsecond instead of nanosecond. Whenever i call the System.nanoTime() function i get a value that ends with 3 zeros.

    For example:
    "1371568052578915000, 1371568052578916000".

    This is what i get everytime. I have not done anything to the variable that stores it before i print it out, and printing out System.nanoTime() directly does not change the problem.

    Here is the code part of the code that i am having problems with. It is not complete yet but i can not imagine anyway that could be relevant to my problem. The "return;" is just temporary.
    Java Code:
    long lastTime = System.nanoTime();
    final double ns = 1000000000.0 / 60.0;
    double delta = 0.0;
    while(running){
    	long now = System.nanoTime();
    	System.out.printf(lastTime + ", " + now);
    	return;
    	//tick();
    	//render();
    }
    Last edited by karl_marx; 06-18-2013 at 05:30 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,864
    Rep Power
    8

    Default Re: Problem with System.nanoTime()

    From the API for System.nanoTime():

    Returns the current value of the running Java Virtual Machine's high-resolution time source, in nanoseconds.

    This method can only be used to measure elapsed time and is not related to any other notion of system or wall-clock time. The value returned represents nanoseconds since some fixed but arbitrary origin time (perhaps in the future, so values may be negative). The same origin is used by all invocations of this method in an instance of a Java virtual machine; other virtual machine instances are likely to use a different origin.

    This method provides nanosecond precision, but not necessarily nanosecond resolution (that is, how frequently the value changes) - no guarantees are made except that the resolution is at least as good as that of currentTimeMillis().

    Differences in successive calls that span greater than approximately 292 years (263 nanoseconds) will not correctly compute elapsed time due to numerical overflow.

    The values returned by this method become meaningful only when the difference between two such values, obtained within the same instance of a Java virtual machine, is computed.

    For example, to measure how long some code takes to execute:

    long startTime = System.nanoTime();
    // ... the code being measured ...
    long estimatedTime = System.nanoTime() - startTime;

    To compare two nanoTime values

    long t0 = System.nanoTime();
    ...
    long t1 = System.nanoTime();

    one should use t1 - t0 < 0, not t1 < t0, because of the possibility of numerical overflow.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    karl_marx is offline Member
    Join Date
    Jun 2013
    Posts
    2
    Rep Power
    0

    Default Re: Problem with System.nanoTime()

    How come that i always get a number that ends with 3 zeros though? This does not seem to be normal for everyone else.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,339
    Blog Entries
    7
    Rep Power
    20

    Default Re: Problem with System.nanoTime()

    Quote Originally Posted by karl_marx View Post
    How come that i always get a number that ends with 3 zeros though? This does not seem to be normal for everyone else.
    It depends on the internal timers used and the operating system ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. System Problem
    By DPdieciocho in forum New To Java
    Replies: 4
    Last Post: 06-17-2013, 02:28 PM
  2. Timer with System.nanotime()
    By ICEpower in forum New To Java
    Replies: 5
    Last Post: 05-01-2013, 04:25 PM
  3. System.nanoTime();
    By rootpi in forum New To Java
    Replies: 6
    Last Post: 01-28-2011, 03:38 AM
  4. Question about System.nanoTime()?
    By chan_nguyen in forum New To Java
    Replies: 5
    Last Post: 09-06-2010, 09:11 AM
  5. problem with System.nanoTime()
    By Bryan500 in forum New To Java
    Replies: 5
    Last Post: 11-01-2008, 03:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •