During the review, I came across a code that is a mixture of Integer and int. For a moment I thought that there would be no problem because it would be converted by auto boxing, but I was wondering what the performance was, so I would like to verify it.
** 1. Pattern with a mixture of reference types and primitive types **
qiita.java
long begin = 0L;
long end = 0L;
begin = System.currentTimeMillis();
Integer sum = 0;
for(int i = 0; i < 1000000; i++) {
sum += i;
}
end = System.currentTimeMillis();
System.out.println(end - begin + "millisecond");
** 2. Primitive type only pattern **
qiita.java
long begin = 0L;
long end = 0L;
begin = System.currentTimeMillis();
int sum = 0;
for(int i = 0; i < 1000000; i++) {
sum += i;
}
end = System.currentTimeMillis();
System.out.println(end - begin + "millisecond");
--Perform 1,000,000 addition processing using the verification source code. The average time is calculated by measuring each three times. Round to the first decimal place.
** 1. Pattern with a mixture of reference types and primitive types **
--First time: 15 ms --Second time: 16 ms --Third time: 15 ms --Average: 15.33 ms
** 2. Primitive type only pattern **
--First time: 5 ms --Second time: 4 ms --Third time: 4 ms --Average: 4.33 ms
The mixed reference and primitive patterns averaged 15.33 ms, while the primitive-only patterns averaged 4.33 ms. It can be seen that it takes about four times as long when autoboxing is working with a mixture of reference types and primitive types. It turns out that if dust accumulates, it will be a mountain, and depending on the number of data, it should not be meaninglessly auto-boxing.
See you again (^_^) Noshi
Recommended Posts