>>> You've likely already spent more time thinking about this
>>> than will be spent boxing and unboxing primitives in
>>> the lifetime of every program you'll ever write during
>>> your career. Don't fear the wrapper classes.
>> My gut feeling is that this is probably false -- autoboxing is /not/ free
>> and if used with large amounts of data, or in contexts where the number of
>> operations is more than linear in the amount of data (as is the case for
>> sorting -- the OP's target application) then it should not be used lightly.
>It's possible that I'm exaggerating, but I've never
>seen anything in a profiler that suggested that it
>might be causing a problem, and I have strong allergies
>to thinking about performance concerns at all during
>normal development.
This depends a LOT on the type of application you're writing. In most
business logic, Chris is completely right - just do what's clearest to the
developers and most provably correct, worrying about microperformance a lot
less than your overall algorithms unless the profiler says otherwise.
>I suspect the JIT compiler does something clever
>in the simpler cases.
Additionally, Sun's wrapper objects cache autobox objects for 256 values
near 0. However, Mark's right too: it's not magic. There _IS_ a hit for
this, and if you're writing an app where you think it's important, you're
probably right, and you should take the time to do the right thing (and
document that you've thought about and covered the edge cases).
>My gut feeling is that inlining Integer.compareTo() is a simpler case, but
>I'd yield to contrary profiling.
Yup, that about covers it.
--
Mark Rafn dagon@dagon.net <http://www.dagon.net/>
Mark Jeffcoat - 14 Nov 2006 19:08 GMT
> This depends a LOT on the type of application you're writing. In most
> business logic, Chris is completely right - just do what's clearest to the
[quoted text clipped - 9 lines]
> probably right, and you should take the time to do the right thing (and
> document that you've thought about and covered the edge cases).
You've switched the references, here: I'm defending the
idea of ignoring any potential performance hit from boxing
primitives.
The key difference of opinion, I suspect, is in your last
paragraph: I think that intelligent, experienced, well-meaning
programmers will be wrong about the magnitude (and occasionally
even the direction) of performance choices like this when
they try to make those decisions ahead of time.
(I admit occasional exceptions; for example, when your
entire program is to implement a single algorithm. Even
so, why cultivate bad habits?)

Signature
Mark Jeffcoat
Austin, TX