Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  System.nanoTime() versus System.currentTimeMillis()  (Read 4079 times)
0 Members and 1 Guest are viewing this topic.
Offline ags1

JGO Wizard


Medals: 67
Projects: 3
Exp: 5 years


Make code not war!


« Posted 2013-05-31 21:17:38 »

I've read all sorts of thing on the internet about how innacurate currentTimeMillis() is on Windows, so I thought I would do on experiment on Windows 7.

I wrote a class that sleeps for less than the putative accuracy of Windows and timed the sleep in nanos and millis. Then I put the ratio of the results (expressed as a percentage) in a table.


public class TryItOut {

    public static void main(String[] args) throws InterruptedException {
        TreeMap<Integer, Integer> multiCounter = new TreeMap<Integer, Integer>();
        for (int i = 0; i < 500; i++) {
            long nowNano = System.nanoTime();
            long nowMilli = System.currentTimeMillis();
            Thread.sleep(10L);
            long nano = System.nanoTime() - nowNano;
            long milli = System.currentTimeMillis() - nowMilli;
            int diff = (int) (nano / milli / 10000);
            Integer count = multiCounter.get(diff) == null ? 1 : multiCounter.get(diff) + 1;
            multiCounter.put(diff,  count);
        }
        for (Map.Entry<Integer, Integer> sortedEntry : multiCounter.entrySet()) {
            System.out.println(sortedEntry.getKey() + ":" + sortedEntry.getValue());
        }
    }

}


Now, before someone points out that Thread.sleep() is itself inaccurate, this is discounted by my loop as I look at the proportional (not the absolute) difference between nanos and millis.

Just to be clear, I am not measuring the accuracy of the timers but only the degree to which the timers agree with each other.

These are the results for a 10ms sleep:


91:2
94:1
96:2
97:2
98:6
99:394
100:87
101:3
103:2
106:1


I would say currentTimeMillis() aint that bad!

The spread of course gets wider as you go for smaller sleeps. This is for 5ms sleeps:


82:1
84:1
85:1
88:1
89:1
90:1
91:1
94:2
95:3
96:2
97:10
98:39
99:352
100:59
101:16
102:5
106:1
109:2
110:1
116:1


...but for 60FPS (17ms) the results get quite tight:


94:2
95:5
96:1
97:3
98:19
99:360
100:84
101:17
102:2
103:5
104:2


My benchmark app uses System.currentTimeMillis() because it measures large timescales of at least a second. The difference between nanos and millis for one second intervals is:


99:149
100:351


EDIT: At the risk of belaboring the point, I ran the 1sec test again, with the ratio measured in thousandths:


998   :   5
999   :   113
1000   :   382

Offline sproingie

JGO Kernel


Medals: 202



« Reply #1 - Posted 2013-06-01 17:27:44 »

You're measuring the precision of system timers using the very system timers that are alleged to be imprecise.  You could see how this might present a problem?  Problem is, getting a high frequency timer that is precise will "fix" the system timer for that process, a sort of Heisenbug if I've ever seen one. 

Measuring cumulative error, which manifests as missed frames, might be more accurate.  I lack the math chops to remember exactly how to measure that sort of thing though Tongue
Offline ags1

JGO Wizard


Medals: 67
Projects: 3
Exp: 5 years


Make code not war!


« Reply #2 - Posted 2013-06-01 17:41:09 »

I'm just using Thread.sleep() to cause a certain delay, which then gets measured using currentTimeMillis() and nanoTime(). Then I look at the difference between the two measures proportionally. So I don't see the problem you see.

Of course, if both measures are inaccurate in the same directions at the same time, my loop would see nothing. But that is not relevant - I am only interested in the question of how divergent the results of nanoTime() and currentTimeMillis() are.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline DQQAYME
« Reply #3 - Posted 2013-06-05 06:37:06 »

http://www.java-gaming.org/topics/java-system-system-currenttimemillis-error/29797/msg/274269/view/topicseen.html
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
            System.out.println(milli);
            int diff = (int) (nano / milli / 10000);
/*
16
15
16
0
Exception in thread "main" java.lang.ArithmeticException: / by zero
   at Main.TryItOut.main(TryItOut.java:18)
*/


same win XP Wink

Offline gene9

Senior Duke


Medals: 10



« Reply #4 - Posted 2013-06-05 20:40:55 »

the test in the OP doesn't seem useful or meaningful.

IRIC, use nanoTime to measure time deltas more precisely, and currentTimeMillis to get a more accurate calendar time stamp value.

If you are benchmarking code, consider one of the microbenchmark frameworks such as jmh: http://openjdk.java.net/projects/code-tools/jmh/.
Offline ags1

JGO Wizard


Medals: 67
Projects: 3
Exp: 5 years


Make code not war!


« Reply #5 - Posted 2013-06-05 22:11:49 »

http://www.java-gaming.org/topics/java-system-system-currenttimemillis-error/29797/msg/274269/view/topicseen.html
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
            System.out.println(milli);
            int diff = (int) (nano / milli / 10000);
/*
16
15
16
0
Exception in thread "main" java.lang.ArithmeticException: / by zero
   at Main.TryItOut.main(TryItOut.java:18)
*/


same win XP Wink

You could try this to force a high precision timer on XP:

http://www.java-gaming.org/topics/yet-another-game-loop/25366/msg/218513/view.html#msg218513

@gene9 the test is meaningful in the sense that it clearly demonstrates the accuracy of currentTimeMillis() on Win7 is better than 16ms (assuming that nanoTime is more accurate than that), which I did not know. I agree it does not tell you anything about the actual accuracy of either timer, but that ws not the question I was asking!

Although... using a sleep forces a high resolution timer in any case (as long as the sleep is longer tha the resolution of the regular timer), so I had better find a different way of forcing a delay. But at least I know that Win7 has a high res timer by default.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (49 views)
2014-10-17 03:59:02

Norakomi (38 views)
2014-10-16 15:22:06

Norakomi (31 views)
2014-10-16 15:20:20

lcass (34 views)
2014-10-15 16:18:58

TehJavaDev (65 views)
2014-10-14 00:39:48

TehJavaDev (65 views)
2014-10-14 00:35:47

TehJavaDev (54 views)
2014-10-14 00:32:37

BurntPizza (72 views)
2014-10-11 23:24:42

BurntPizza (43 views)
2014-10-11 23:10:45

BurntPizza (84 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!