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

Performance Comparisons between "int" and "long"

This section provides a performance comparisons between 'int' and 'long' loops and basic operations in interpreted-only mode. 'int' runs much faster.

If you compare testing results between "int" and "long" loops, you will see that: loops with "long" indexes run much slower than "int" indexes as bytecodes (interpreted-only mode) with HotSpot JVM. Here are comparisons of execution times per step in nanoseconds:

                  int   long
Empty loop         16     25
Assignment: x=i    14     24
Shift: x=i<<1      17     30
Add: x=i+i         17     34
Multipply: x=i*3   17     38
Divide: x=i/3      19     53

By subtracting assignment loop execution time from shift, add, multiply and division tests, we can get execution estimates for shift, add, multiply and division operations in nanoseconds:

                  int   long
Shift: i<<1         3      6
Add: i+i            3     10
Multipply: i*3      3     14
Divide: i/3         5     29

Conclusion: Use "int" data type instead of "long" as much as you can to get better execution performance in interpreted-only mode.

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
Performance Comparisons between "int" and "long"