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

 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

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

 Outdated Tutorials


 PDF Printing Version