Re: Getter performance
Daniel Pitts wrote:
On 10/21/11 2:27 PM, Roedy Green wrote:
I think you should know without benchmarking each individual case
which Map or Collection would be best for a given task.
I nearly agree.
An experienced programmer should know which Map or Collection would work
sufficiently well for a given task. "Best", by some definitions, would
almost always be a custom class (perhaps not even implementing Map or
Collection). The thing is that best may be much better than needed.
Also, even between two algorithms, "best" may require a lot of
experiments, and be machine dependent.
For example, consider binary vs. linear search of a sequential array.
Binary search does O(log n) comparisons, but with unpredictable
conditional branches leading to a high cost per branch. Linear search
does O(n) comparisons, but most of its conditional branches go the same
way, making them relatively cheap.
They are of about equal coding cost, because Arrays has methods to do
binary search, and linear search is extremely simple.
For very short arrays I would definitely use linear search. For very
large arrays I would definitely use binary search. How about an array
length 100? Which is faster?
I think either would be "sufficiently good" except in very rare
conditions. If I had a program with a performance bottleneck on a search
of a length 100 array, I would measure in the context of that program.
Patricia