"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:

Here is how my empty loop test method look like:

/**
 * BenchmarkTestInt.java
 * Copyright (c) 2010, HerongYang.com, All Rights Reserved.
 */
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:

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

 Downloading and Installing JDK 1.8.0 on Windows

 Downloading and Installing JDK 1.7.0 on Windows

 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 28.2.7 by Oracle Corporation

 JVM Runtime Data Areas

 Memory Management and Garbage Collectors

 Garbage Collection Tests

 JVM Stack, Frame and Stack Overflow

 Thread Testing Program and Result

 CPU Impact of Multi-Thread Applications

 I/O Impact of Multi-Thread Applications

 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

 Outdated Tutorials

 References

 PDF Printing Version