Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integer auto-unboxing and auto-boxing gives performance issues?

We are currently doing some iterations and other operations using x++; where x is an Integer and not an int.

Operations may be repeated throughout some user operations on our system but nothing too complex or numerous like a Mathematical application, maximum up to 10000 times per user transaction..

Will this unboxing and later boxing affect our performance by some noticeable milliseconds?

like image 706
camiloqp Avatar asked May 17 '11 21:05

camiloqp


People also ask

How performance is affected due to boxing and unboxing?

During process of boxing and unboxing, it may be possible that the performance of conversion is being affected due the large number of items stored in a collection. I've done a bit of research over this and here is my conclusion. As a general conclusion, we can say that an object is equivalent to a 'void *' of c++.

What happens when an int is Autoboxed?

Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. For example, converting an int to an Integer, a double to a Double, and so on. If the conversion goes the other way, this is called unboxing.

What are auto boxing and auto unboxing give an example of how they might cause an exception to be thrown?

The term autoboxing refers to the auto conversion of primitive type to its correspond object type. For example, conversion of int type to Integer object or char type to Character object. This conversion is done implicitly by the Java compiler during program execution.

What is the major advantage of auto boxing?

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.


2 Answers

http://download.oracle.com/javase/1.5.0/docs/guide/language/autoboxing.html

"The performance of the resulting list is likely to be poor, as it boxes or unboxes on every get or set operation. It is plenty fast enough for occasional use, but it would be folly to use it in a performance critical inner loop.

So when should you use autoboxing and unboxing? Use them only when there is an “impedance mismatch” between reference types and primitives, for example, when you have to put numerical values into a collection. It is not appropriate to use autoboxing and unboxing for scientific computing, or other performance-sensitive numerical code. An Integer is not a substitute for an int; autoboxing and unboxing blur the distinction between primitive types and reference types, but they do not eliminate it."

like image 91
Erhan Bagdemir Avatar answered Nov 17 '22 00:11

Erhan Bagdemir


Yes, there is a performance impact. The equivalent code produced for ++x involves creating a new Integer object each time. x++ additionally creates a temporary variable to store the previous integer reference, and some manipulation. You can verify this by disassembling the class file.

like image 36
Raze Avatar answered Nov 16 '22 22:11

Raze