If you are using ArrayList (or any type of collection ) and adding a lot
objects, set the size of the ArrayList to the largest number you can think
of. Example, if you think you might insert 20000 objects into the List,
then
create the ArrayList with 20000 entries.
How many would be "a lot"?
How about reading records from a ResultSet and no expectation the number
of records there?[/QUOTE]
That is actually the easy case - profile!
If you instrument your code with unit tests, you might build a few tests
designed to test the runtime performance. Make up a test dataset that
returns the number of records you feel you need to handle elegantly, and
also make up test sets for "typical" and "minimal" cases, and set
definite requirements.
If you decide that your test set will work very quickly for 0 to 50
returned objects, but that it should handle 100, 1000, and 10000 without
breaking a sweat, and that it should handle records with a dozen or two
dozen fields without a problem, but you will handle 256 VARCHAR255's
full of data, then your test suite is pretty easy to build. Further, by
going an order of magnitude beyond your anticipated needs, you can
usually spot n^2 problems early in the design phase.
I am a fan of not optimizing until I have a use case that shows me
_what_ to optimize. Just trying to "make things faster" rarely is a
good idea, unless you have a really clear image of what the code is
supposed to do, and once you have that, you might as well translate that
image into definitive test cases run on every build.
Scott