Consumed Time of System.out.println(); Java statement?

debugging, java, monitoring

Solution

`System#currentTimeMillis` returns `long` and not `double`. Thus you're loosing `.2`.

Testing 5 statements is not a good idea, specially when you almost don't feel the time it takes to perform it. I advise you to have more than 5 statements to test and then reduce the amount to something more than 1 as well.

You want to do a precise measurements time, it's better to use `System#nanoTime`, since it gives time in nano seconds:

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

Look for "nanoTime vs currentTimeMillis" and you'll get hundreds of articles.

Problem

I hear that the `System.out.println();` Java statement is costly (it consumes a lot of time) So I try to evaluate its cost: When I evaluate 5 statements... `The cost = 1.0` So I expect the cost of 1 statement = 0.2 But actually I found `The cost = 0.0` !! ``` double t1 = 0; double t2 = 0; t1 = System.currentTimeMillis(); System.out.println("aa"); System.out.println("aa"); System.out.println("aa"); System.out.println("aa"); System.out.println("aa"); t2 = System.currentTimeMillis(); System.out.println("The cost = " + (t2-t1) ); // The cost = 1.0 t1 = System.currentTimeMillis(); System.out.println("aa"); t2 = System.currentTimeMillis(); System.out.println("The cost = " + (t2-t1) ); // The cost = 0.0 // Expected : 1.0/5 = 0.2 -- But Actual : 0.0 ``` Why that?

Original source