Java performance hints and tips?

Discussion in 'Java' started by Ahmed Moustafa, Jul 14, 2003.

  1. 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
    Ahmed Moustafa, Jul 14, 2003
    #1
    1. Advertising

  2. Ahmed Moustafa

    J Guest

    On Mon, 14 Jul 2003 18:44:57 -0400, Ahmed Moustafa wrote
    (in message <JnGQa.100951$>):

    > 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
    J, Jul 15, 2003
    #2
    1. Advertising

  3. Ahmed Moustafa

    steve souza Guest

    steve souza, Jul 15, 2003
    #3
  4. >>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.
    >>

    >
    > 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?

    > You can do this with the StringBuffer also.
    Ahmed Moustafa, Jul 15, 2003
    #4
  5. Ahmed Moustafa

    Jon Skeet Guest

    Jon Skeet, Jul 15, 2003
    #5
  6. Ahmed Moustafa, Jul 15, 2003
    #6
  7. Ahmed Moustafa

    Scott Yanoff Guest

    Scott Yanoff, Jul 15, 2003
    #7
  8. Ahmed Moustafa

    Ike Guest

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

    "Jon Skeet" <> wrote in message
    news:...
    > Ike <> wrote:
    > > http://www-2.cs.cmu.edu/~jch/java/optimization.html

    >
    > 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.
    >
    > --
    > Jon Skeet - <>
    > http://www.pobox.com/~skeet/
    > If replying to the group, please do not mail me too
    >
    Ike, Jul 15, 2003
    #8
  9. In article <JeLQa.2536$>,
    Ahmed Moustafa <> wrote:

    > >>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.
    > >>

    > >
    > > 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?


    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
    Scott Ellsworth, Jul 15, 2003
    #9
  10. 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?
    Ahmed Moustafa, Jul 16, 2003
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark C
    Replies:
    0
    Views:
    316
    Mark C
    Feb 2, 2007
  2. Replies:
    4
    Views:
    321
  3. Replies:
    2
    Views:
    442
  4. Replies:
    1
    Views:
    183
    Dave Burt
    Mar 24, 2005
  5. Replies:
    1
    Views:
    3,040
    Arne Vajhøj
    Jul 31, 2012
Loading...

Share This Page