JVM Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 4.10

"long" Empty Loop: 25 Nanoseconds per Step

This section provides a tutorial example on how to perform benchmark tests to find out how long each step will take to run in an empty loop with a 'long' index in interpreted-only mode. The answer is 25 nanoseconds!

After finishing on testing "int" operations, I want to see performances of "long" operations. Again, I use an empty loop with a "long" index as the first test:

/**
 * BenchmarkTestLong.java
 * Copyright (c) 2010 by Dr. Herong Yang, herongyang.com
 */
class BenchmarkTestLong {

   // The empty loop benchmark test method
   public static long emptyLoop(int steps, BenchmarkRunner runner) {
      long x = 0;
      long i = 0;
      long last = steps;
      runner.startTimer();
      for (i=0; i<last; i++) {
      }
      runner.stopTimer();
      x = i;
      return x;
   }
}

Here are test results:

C:\herong\jvm>java -Xint -Xms100m -Xmx100m BenchmarkRunner 
BenchmarkTestLong emptyLoop 10000 100 10
...
Runs: 100, Ave: 252, Min: 223, Max: 279 - Per step in nanoseconds

C:\herong\jvm>java -Xint -Xms100m -Xmx100m BenchmarkRunner 
BenchmarkTestLong emptyLoop 10000 100 100
...
Runs: 100, Ave: 48, Min: 45, Max: 50 - Per step in nanoseconds

C:\herong\jvm>java -Xint -Xms100m -Xmx100m BenchmarkRunner 
BenchmarkTestLong emptyLoop 10000 100 1000
...
Runs: 100, Ave: 27, Min: 27, Max: 28 - Per step in nanoseconds

C:\herong\jvm>java -Xint -Xms100m -Xmx100m BenchmarkRunner 
BenchmarkTestLong emptyLoop 10000 100 10000
...
Runs: 100, Ave: 25, Min: 25, Max: 27 - Per step in nanoseconds

C:\herong\jvm>java -Xint -Xms100m -Xmx100m BenchmarkRunner 
BenchmarkTestLong emptyLoop 10000 100 100000
...
Runs: 100, Ave: 25, Min: 25, Max: 27 - Per step in nanoseconds

C:\herong\jvm>java -Xint -Xms100m -Xmx100m BenchmarkRunner 
BenchmarkTestLong emptyLoop 10000 100 1000000
...
Runs: 100, Ave: 25, Min: 25, Max: 27 - Per step in nanoseconds

Conclusions based on the test result:

  • The execution time of an empty loop with a "long" index is about 25 nanoseconds per step in interpreted-only mode. This conclusion can be arrived from test results of 10000, 100000 and 1000000 steps. The execution time per step is probably the amount of time for incrementing the loop index and checking loop termination condition.
  • If I convert the per step execution time into my CPU clock clicks, it will be: 25 / 0.5 = 50 clicks, because my CPU clock speed is 2GHz = 0.5 nanoseconds per click.
  • The loop overhead is about 2000 to 2270 nanoseconds. This can calculated using test results of 10, 100 and 1000 steps. For example, if the average execution time is 252 per step including overhead for 10 steps, and we know the actual execution time per step is 25 nanoseconds, so the overhead is: 252*10 - 25*10 = 2270 nanoseconds.
  • This test also confirms that the Java bytecode compiler "javac" is not doing any optimization to replacing the empty loop with "i=steps" which is the net effect of the loop.

I also noticed that during the execution of the 1000000-step test, my laptop CPU cooling fan ran continuously. The cooling fan stopped gradually after the test was done. This tells me that my 1000000-step test was really CPU intensive and caused its temperature to raise!

Last update: 2010.

Table of Contents

 About This Book

 Download and Install Java SE 1.6 Update 2

 java.lang.Runtime Class - The JVM Instance

 java.lang.System Class - The Operating System

 ClassLoader Class - Class Loaders

 Class Class - Class Reflections

 Sun's JVM - Java HotSpot VM

 JRockit JVM 7.0 by BEA Systems

 JRockit JVM 8.0 by BEA Systems

 Memory Management Rules and Tests

 Garbage Collection Tests

 Stack Overflow Tests

 Thread Testing Program and Result

 StringBuffer Testing Program and Result

 CDS (Class Data Sharing)

 Micro Benchmark Runner and JVM Options

 Micro Benchmark Tests on "int" Operations

Micro Benchmark Tests on "long" Operations

"long" Empty Loop: 25 Nanoseconds per Step

 "long" Assignment Only: 24 Nanoseconds per Step

 "long" Shift and Assignment: 30 Nanoseconds per Step

 "long" Add and Assignment: 34 Nanoseconds per Step

 "long" Multiply and Assignment: 38 Nanoseconds per Step

 "long" Division and Assignment: 53 Nanoseconds per Step

 Performance Comparisons between "int" and "long"

 Micro Benchmark Tests in JIT Compilation Mode

 Micro Benchmark Tests on "float" and "double" Operations

 References

 PDF Printing Version

Dr. Herong Yang, updated in 2010
"long" Empty Loop: 25 Nanoseconds per Step