Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autoboxing versus manual boxing in Java

Tags:

Why is the second piece of code faster?

Map<Integer, Double> map = new HashMap<Integer, Double>(); for (int i = 0; i < 50000; i++) {     for (double j = 0.0; j < 10000; j++) {         map.put(i, j);     } }  Map<Integer, Double> map=new HashMap<Integer, Double>(); for (int i = 0; i < 50000; i++) {     for (double j = 0.0; j < 10000; j++) {                     map.put(new Integer(i), new Double(j));     } } 
like image 728
Andrei N Avatar asked Feb 21 '10 23:02

Andrei N


People also ask

What is the difference between boxing and Autoboxing in Java?

You can box an int manually using Integer. valueOf , or you can assign your int value to an Integer variable and it will be autoboxed.

Is Autoboxing also known as boxing?

The automatic conversion of primitive data types into its equivalent Wrapper type is known as boxing and opposite operation is known as unboxing.

What are the advantages of Autoboxing and unboxing in Java?

Autoboxing and unboxing lets developers write cleaner code, making it easier to read. The technique lets us use primitive types and Wrapper class objects interchangeably and we do not need to perform any typecasting explicitly.

What is Autoboxing in Java w3schools?

Autoboxing is the process by which a primitive type is automatically encapsulated (boxed) into its equivalent type wrappers whenever an object of the type is needed.


2 Answers

Autoboxing uses Integer.valueOf, which internally caches Integer objects for small integers (by default -128 to 127, but the max value can be configured with the "java.lang.Integer.IntegerCache.high" property - see the source code of Integer.valueOf), so it is different from calling new Integer directly. Because Integer.valueOf does a quick check for the magnitude of the integer value before calling new Integer, it's a little bit faster to call new Integer directly (though it uses more memory if you have lots of small integers). Allocation in Java is very fast, and the time doing GC is proportional to the number of live short-lived objects (i.e. not proportional to the amount of garbage), so GC is also very fast.

But depending on the JVM version and which optimizations are enabled, there is the scalar replacement optimization, which can in produce a much bigger performance difference when allocating short-lived objects (in your example that optimization can't be done, because you are storing the objects in a map, but in many other situations it's useful).

In recent JVM versions there is scalar replacement optimization (except in 1.6.0_18 where escape analysis is temporarily disabled), which means that allocations of short-lived objects can be optimized away. When scalar replacement in JVM was new, somebody made a benchmark where there was code similar to yours. The result was that the code which used primitives was fastest, the code with explicit new Integer() calls was nearly as fast as the one using primitives, and the code which used autoboxing was much slower. This was because autoboxing uses Integer.valueOf and at least back then scalar replacement optimization did not take that special case into consideration. I don't know whether the optimization has been improved since then.

like image 57
Esko Luontola Avatar answered Nov 02 '22 06:11

Esko Luontola


Autoboxing will use Integer.valueOf and Double.valueOf. There is some overhead in calling those methods (although it will eventually get inlined). Also Integer.valueOf does some checking for low-values to use pooled instances, which is not frequently a win in your code (although it could reduce heap size a little). Pooled instances may be a win where they reduce heap size, GC times and could even improve equality test performance..

But, in general, it's a microoptimisation which you should, in general, ignore.

like image 24
Tom Hawtin - tackline Avatar answered Nov 02 '22 08:11

Tom Hawtin - tackline