Search code examples
javaperformancefor-loopreal-timebenchmarking

How long it takes for one second in Java? Measure latency time in Java


I don't want to change this code, I'm only interested in JVM, OS or kernel customization/configuration for best results!


I have one second loop (1000 x 1ms)

public static void main(String[] args) throws InterruptedException {
    long start = System.nanoTime();
    for (int i = 0; i < 1000; i++ ) {
        Thread.sleep(TimeUnit.MILLISECONDS.toMillis(1));
    }
    long duration = System.nanoTime()  - start;
    System.out.println("Loop duration " + 
         duration / TimeUnit.MILLISECONDS.toNanos(1) + " ms.");
}

On my Fedora 20 with kernel 3.12 this loop needs 1055 ms.

This is pretty good result, average is more than 1100ms.

Is possible to make this code faster with custom JVM flags or OS configuration?

Loop duration 1055 ms.

Solution

  • Unfortunately, my question was misunderstood.

    Realtime java is abandoned, so advice for using Realtime java is not valid.

    After some researches this test has best results on some Windows machines.

    On tested Windows 8.1 this tests prints exactly 1000ms.

    Other results:

    • Mac Os X 10.9.1 with Java 1.7.0_25 : 1180 - 1190ms
    • Ubuntu 12.04/Corei3/4GB : 1122 ms

    References