Re: why people use "Map m= new HashMap()" or "List l = new ArrayList()"?
Roedy Green wrote:
On Sat, 20 Oct 2007 11:43:26 +0100, "Daniel Dyer" <"You don't need
it"> wrote, quoted or indirectly quoted someone who said :
I doubt you'd ever notice the difference. Let the JIT take care of
optimising this rather than doing it explicitly.
What evidence do you have that it is possible to optimise away the
extra overhead of interface references? When you think about what has
to happen under the hood it would be quite a feat.
What evidence do you have that it won't?
The problem with optimization discussions is that, by all experienced
accounts, only actual measurement of a specific application's performance will
effect an accurate assessment of what to optimize for a given program on a
given JVM.
I have seen measured jumps of 50% in performance just by switching to a
"-server" JVM configuration for the exact same applications. Some JVMs do
escape analysis and massive inlining /at runtime/ of heavily-run segments of
code. Published benchmarks show a block of code speeding up as the optimizer
"settles in" to the usage pattern.
You cite Knuth as if choice of an interface variable over a class variable
were always an algorithmic decision, whereas in many cases the choice of, say,
a List implementation is irrelevant to the particular algorithm. Knuth warned
against the effects of premature optimization, which by all accounts is
defined as prior to measurement of the actual performance.
Naturally if the overhead of interface calls over class calls is an important
factor, then it makes sense to narrow the type of the relevant variables.
--
Lew