0

Consider the following code

package tests;

public class Test_computer_speed {

    public static void main(String[] args) {

        for(int i=0; i<10; ++i) {
            testMillis();
        }

        for(int i=0; i<10; ++i) {
            testNanos();
        }

    }

    public static void testMillis() {


        long startTime = System.currentTimeMillis(), endTime = startTime + 6000, iterations = 0, now;
        while (true) {
            now = System.currentTimeMillis();
            if (now > endTime)
                break;
            iterations++;
        }

        System.out.println("In 6 seconds, the loop performed " + iterations + " iterations.");

    }

    public static void testNanos() {


        long startTime = System.nanoTime(), endTime = startTime + 6000000000l, iterations = 0, now;
        while (true) {
            now = System.nanoTime();
            if (now > endTime)
                break;
            iterations++;
        }

        System.out.println("In 6 seconds, the loop performed " + iterations + " iterations.");

    }

}

output follows:

>java tests.Test_computer_speed
In 6 seconds, the loop performed 1402955969 iterations.
In 6 seconds, the loop performed 1452564156 iterations.
In 6 seconds, the loop performed 1443087123 iterations.
In 6 seconds, the loop performed 1417403052 iterations.
In 6 seconds, the loop performed 1451880195 iterations.
In 6 seconds, the loop performed 1444997459 iterations.
In 6 seconds, the loop performed 1438911291 iterations.
In 6 seconds, the loop performed 1461714025 iterations.
In 6 seconds, the loop performed 1460849690 iterations.
In 6 seconds, the loop performed 1441407031 iterations.
In 6 seconds, the loop performed 512124051 iterations.
In 6 seconds, the loop performed 524528348 iterations.
In 6 seconds, the loop performed 525100685 iterations.
In 6 seconds, the loop performed 528140105 iterations.
In 6 seconds, the loop performed 526142121 iterations.
In 6 seconds, the loop performed 526086430 iterations.
In 6 seconds, the loop performed 530290126 iterations.
In 6 seconds, the loop performed 529469812 iterations.
In 6 seconds, the loop performed 528229751 iterations.
In 6 seconds, the loop performed 529689093 iterations.

This means that nanoTime() function is 10 times slower, than currentTimeMillis.

What is the sense of having more precise function if it is slower?

In other words, Java is cheating, probably gaining precision by waiting for some time on call.

UPDATE

Look at uncertainty. The nanoTime() speed is 10 times slower, and the uncertainty is 10 times better.

This means, that absolute uncertainty IS THE SAME.

Dims
  • 47,675
  • 117
  • 331
  • 600
  • 2
    10 times slower? 1441407031/512124051 is approximately 2.8, not 10. And why is it surprising that more precision requires more work to calculate? – Louis Wasserman May 06 '15 at 17:23
  • 1
    1460849690 / 512124051 = 2.8 times slower? Your point still stands that it's slower, but 2.8 is very different from 10. – Mike Ounsworth May 06 '15 at 17:24
  • I was rounding to the order of magnitude (number of digits) – Dims May 06 '15 at 17:49
  • @LouisWasserman where do you see more precision? Number of loops is uncertain to the same level – Dims May 06 '15 at 17:51
  • @Dims I mean that `System.nanoTime()` returns, at least potentially, a more precise value, which may require more work to compute. – Louis Wasserman May 06 '15 at 17:52
  • @LouisWasserman imagine you have very precise scalpel, which can cut with nanometer precision; unfortunately, the thickness of this scalpel is 10 millimeters; and you are saying, that it is normal to have more precise scalpel being more thick; I mean that the only use of such scalpel is to use as guillotine; you can't do helpful surgery with it, you can just cu head and be sure, that the length of the remaining neck stump is the precise number of nanometers :) – Dims May 06 '15 at 17:58
  • 1
    @Dims read the duplicate question, which explains the more expensive API required to get more precise results. – Louis Wasserman May 06 '15 at 17:59
  • @LouisWasserman the problem is that losses are of the same value as gains; imagine you need to pay 100 dollars to save 10 cents – Dims May 06 '15 at 18:02
  • 1
    `System.currentTimeMillis()` doesn't actually have millisecond precision, though; it's not accurate to the millisecond. In any event, based on your numbers, `nanoTime()` takes approximately 11 nanoseconds, which is still better resolution than `currentTimeMillis` could possibly provide. – Louis Wasserman May 06 '15 at 18:04
  • Your test is invalid. You're measuring `currentTimeMillis()` with millisecond accuracy, but `currentTimeNanos()` with nanosecond accuracy. You're getting a major granularity problem which is skewing your results by up to three orders of magnitude. – user207421 May 06 '15 at 18:52

0 Answers0