Primitives and Objects Benchmark in Java
I was told many times that Java primitives are better and faster than objects, but I haven’t found any benchmarks for that. So I made my own. I was trying to check how much slower it is to use the objects like Integer
, Long
, Double
instead of int
, long
, or double
.
The Benchmark Code
The benchmark code is really simple, I’m not gonna publish it here. First of all I’m not fully happy with the code, it could be a little bit nicer. Another reason is that if somebody wants to improve the results, then the implementation could be done some other way and this could be much better.
The main test function looks like this:
public void test(int maxLoops) {
int max = maxLoops;
for (int index = 0; index < max; index++) {
int number = index * index + index * index;
}
}
The above is the int
version. The Integer
version looks like this:
public void test(int maxLoops) {
Integer max = maxLoops;
for (Integer index = 0; index < max; index++) {
Integer number = index * index + index * index;
}
}
Other versions are very similar, just leave the int
parameter as it is and change the rest with the type you want.
There is some logic for running the tests, but it is not very important.
The Test Machine
My test machine is my old notebook.
64bit Ubuntu Linux 10.10 (maverick)
Linux 2.6.35-23-generic
4GB RAM
Intel Core2 Duo T8100 @ 2.10GHz
Java is a normal Java from repositories, installed automatically by apt-get something
.
java version "1.6.0_22"
Java(TM) SE Runtime Environment (build 1.6.0_22-b04)
Java HotSpot(TM) 64-Bit Server VM (build 17.1-b03, mixed mode)
The Results
The Dry Run
First of all I simply run that code, this would be quite not optimal as Java Virtual Machine can optimize that later by recompiling the functions. These should be quite useless results, but it should be nice to observe the improvements.
First Two Runs
I run the test functions two times, to check if JVM can optimize something.
Variable Type | First Run [ms] | Second Run [ms] | Difference [ms] | Difference [%] |
---|---|---|---|---|
int | 5 | 1 | -4 | -80.00 |
Integer | 2686 | 2398 | -288 | -10.72 |
double | 541 | 542 | +1 | +0.18 |
Double | 2399 | 2383 | -16 | -0.67 |
long | 66 | 66 | 0 | 0.00 |
Long | 2408 | 2402 | -6 | -0.25 |
The difference between primitives and object types:
Variable Types | First Run | Second Run |
---|---|---|
int / Integer | 537.00 | 2398.00 |
double / Double | 4.43 | 4.40 |
long / Long | 36.48 | 36.39 |
Numbers here shows how many times slower are the object versions.
The Conclusions
Java really optimized some of the functions between the runs, the biggest difference between the runs was in int
case.
As you can see, object versions are really slower than appropriate primitive ones.
I really wasn’t expecting so huge difference in the int/Integer
case.
Let’s Heat That Up
Java Virtual Machine has something called HotSpot:
HotSpot is the primary Java Virtual Machine for desktops and servers, produced by Oracle Corporation. It features techniques such as just-in-time compilation and adaptive optimization designed to improve performance.
To check that, you have to run the code you want. This allows JVM to gather enough information to optimize the code. I’m interested in the difference in code that runs for ages, so let’s give the HotSpot a chance and run the test code enough times.
Test procedure now looks like this:
- First dry run
- Second dry run
- Run each function 100,000,000 times with 100,000 as parameter
- First function run - with 100,000 as parameter
- Run each function 100,000,000 times with 100,000 as parameter
- Second function run - with 100,000 as parameter
Results for the first and second bigger run:
Variable Type | First Run [ms] | Second Run [ms] | Average |
---|---|---|---|
int | 1 | 1 | 1 |
Integer | 2399 | 2405 | 2402 |
double | 247 | 242 | 245 |
Double | 2448 | 2367 | 2408 |
long | 64 | 66 | 65 |
Long | 2404 | 2430 | 2417 |
As both times are very similar, I just took the average of both measurements.
Comparing the HotSpot optimization
Variable Type | First Dry Run [ms] | Second HotSpot Run [ms] | Difference [ms] | Difference [%] |
---|---|---|---|---|
int | 5 | 1 | 4 | 80.00% |
Integer | 2686 | 2402 | 284 | 10.57% |
double | 541 | 245 | 296 | 54.71% |
Double | 2399 | 2408 | -9 | -0.38% |
long | 66 | 65 | 1 | 1.52% |
Long | 2408 | 2417 | -9 | -0.37% |
So it looks like the HotSpot made quite a good job optimizing the int
and the double
version. The long
one also looks nice now, but not so impressing.
The Final Results
What is the difference between the primitives and the objects after the HotSpot play?
Variable Types | Ratio |
---|---|
int / Integer | 2402.00 |
double / Double | 9.83 |
long / Long | 37.18 |
Well, I wasn’t expecting so huge difference. It seems that primitives are really faster. What’s more, int
is really faster than Integer
. The difference between doubles
and longs
is not so big, but it is still there.
The Final Conclusion
Yes, you should really use primitives, wherever you can. Of course you can’t use that everywhere, but if you can - you should.