Java Performance Tuning

Jack Shirazi

Mentioned 3

Helps readers eliminate performance problems, covering topics including bottlenecks, profiling tools, strings, algorithms, distributed systems, and servlets.

More on

Mentioned in questions and answers.

What's the best/preferred idiom for a decrementing for loop, and why?

for(int idx=(len-1); idx>=0; idx--) {...}


for(int idx=(len-1); idx>-1; idx--) {...}

or (based on sylvarking's answer, but using for to constrain the index scope)

for(int idx=len; idx-->0; ) {...}

Is one or the other more likely to trip up another developer?

I recommend the following because comparison against zero is optimized at the byte code level.

for(int idx=(len-1); idx>=0; idx--) {...}

This is a suggestion from Java Performance Tuning by Jack Shirazi

We recently upgraded to 10g and I've noticed that metadata queries seem to take an extraordinarily long time (at a guess 2-5 times slower than on 9i). Has anyone else noticed this? Any suggestions on ways to rectify this problem (settings/patches/incantations)?


If you have access to Oracle Metalink (MyOracleSupport, or whatever they're calling it today), look up bug #2475998, DRAMATIC DROP IN PERFORMANCE USING SYNONYMS VIA SETINCLUDESYNONYMS METHOD. The submitter reported a 60x degradation of performance of DatabaseMetadata.getColumns() when synonyms exist. Oracle recommends getting metadata in some other way e.g. execute a dummy query and get ResultSetMetaData.

Also see Jack Shirazi's Java Performance Tuning, which has a section on performance of obtaining database metadata. (link to Google Books excerpt)

Bottom line is, don't use DatabaseMetadata.getColumns().

I was using these in my code but I think they are may be not as fast as manual coded procedure. I had searched the and found some articles which say that System.arraycopy() is actually faster than copying an array manually. I am not quite sure whether that is correct or not.

Also, the function Array.sort() is the fast compared to what we write in code?

// I am merging the arrays here into a new integer array called newarray3 
    int[] newarray3= new int[input1.length + input2.length];
    System.arraycopy(input1, 0, newarray3, 0, input1.length);
    System.arraycopy(input2, 0, newarray3, input1.length, input2.length);

    //sorting the array.

input1 and input2 are two arrays which are to be merged and then sorted. I want to know whether coding this way is making my program slower. Or could it be something else. Please help.

arrayCopy() is a native method, so yes it might be faster than a pure Java implementation coded by hand. On the other hand, sort() is a pure java method, but designed for generic sorting - and the specific algorithm used depends on the data type of the array, take a look at this post to understand the details.

You could make your own sorting implementation that's faster by improving the comparison between objects and specializing the algorithm for a certain data type, in fact this is the approach recommended in the book Java Performance Tuning. Anyway, you won't know for sure until a profiler is used for comparison.