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

"int" Empty Loop: 16 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 in interpreted-only mode. The answer is 16 nanoseconds!

My first goal is to find out how long it will take to run an empty loop with an "int" index in interpreted-only mode. I used these parameters to control my tests:

  • "-Xint" JVM option to stop JIT compilations and keep my test in interpreted-only mode.
  • "-Xms100m -Xmx100m" JVM options to allocate enough memory to avoid GC.
  • "warmups=10000" benchmark runner parameter to ensure the JVM is warm enough to be stable.
  • "runs=100" benchmark runner parameter to repeat the test 100 times to average out interruptions from the operating system.
  • "steps=10, 100, 1000, 10000, 100000, and 1000000" benchmark runner parameters to see the impact of the loop overhead.

Here is how my empty loop test method look like:

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

   // The empty loop benchmark test method
   public static int emptyLoop(int steps, BenchmarkRunner runner) {
      int x = 0;
      int i = 0;
      int 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 
BenchmarkTestInt emptyLoop 10000 100 10
...
Runs: 100, Ave: 239, Min: 196, Max: 252 - Per step in nanoseconds

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

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

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

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

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

Conclusions based on the test result:

  • The execution time of an empty "int" loop is about 16 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: 16 / 0.5 = 32 clicks, because my CPU clock speed is 2GHz = 0.5 nanoseconds per click.
  • The loop overhead is about 2000 to 2300 nanoseconds. This can calculated using test results of 10, 100 and 1000 steps. For example, if the average execution time is 39 per step including overhead for 100 steps, and we know the actual execution time per step is 16 nanoseconds, so the overhead is: 39*100 - 16*100 = 2300 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

 Revised BenchmarkRunner.java

 Hardware, OS and JVM Configurations

"int" Empty Loop: 16 Nanoseconds per Step

 "int" Assignment Only: 14 Nanoseconds per Step

 "int" Shift and Assignment: 17 Nanoseconds per Step

 "int" Add and Assignment: 17 Nanoseconds per Step

 "int" Multiply and Assignment: 17 Nanoseconds per Step

 "int" Division and Assignment: 19 Nanoseconds per Step

 Micro Benchmark Tests on "long" Operations

 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
"int" Empty Loop: 16 Nanoseconds per Step