Java performance hints and tips?

A

Ahmed Moustafa

Is there complied list of the hints and tips for fast and improved
performance Java code? e.g. something like: in concatenation use
StringBuffer instead of String.

Thanks in advance,
Ahmed
 
J

J

Is there complied list of the hints and tips for fast and improved
performance Java code? e.g. something like: in concatenation use
StringBuffer instead of String.

Thanks in advance,
Ahmed

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.

You can do this with the StringBuffer also.

J
 
A

Ahmed Moustafa

Is there complied list of the hints and tips for fast and improved
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?
 
I

Ike

Yikes! I hadnt even reaslise...thanks. -Ike

Jon Skeet said:
Note that that site has been "retired" for over 5 years, so I would
take all comparitive benchmarks on there as almost certainly bogus now.
 
S

Scott Ellsworth

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
 
A

Ahmed Moustafa

When a PreparedStatement gets created and then the Connection is closed,
does the database keep the PreparedStatement somewhere? If so, when the
statement gets created again, how can the database related the old
complied one to the new one?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top