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

camiloqp picture camiloqp · May 17, 2011 · Viewed 7.6k times · Source

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?

Answer

Erhan Bagdemir picture Erhan Bagdemir · May 17, 2011

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."