Translate

Sunday, November 18, 2012


Still continue Java Performance Tuning-4 also

There are several ways to make a faster loop. Don’t terminate loop with method calls/ property assessors.

Eliminate method / property call

Byte x[] = new byte[loop];
For(int i=0; i
{
          for(int j=0;j
          {
          }
}                                                                                     Takes 109ms


byte x[] = new byte[loop];
int length = x.length;
for(int i=0;j
{
          for(int j=0; j
          {
          }                                                                            Takes 62 ms
}


Eliminate method / property call

Method / property call generates some overhead in object oriented paradigm.

Loop Optimization:

Use int to iterate over loop.

For(int i=0;i
{
          for(int j=0;j
          {
          }
}                                                                                     Takes 62 ms


for(short i=0;i
{
          for(short j=0;j
          {
          }
}                                                                                     Takes 125 ms

Vm is optimized to use int for loop iteration not by byte, short, char use System.arraycopy(…) for copying object instead of running over loop

System.arrayCopy(..)

For(int i=0; i
{
          x[i]=y[i];
}                                                                                        Takes 62 ms



System.arraycopy(x,0,y,0,x.length())                                Takes 16 ms


System.arraycopy(…) is a native method, avoids double interpretation and hence performs better Terminate loop by primitive literal, and not by method call or variable


Terminate loop by primitive

For(int i=0;i
{
          for(int j=0;j
          {
          }
}                                                                                     Takes 424 ms




for(int I =countArr.length-1;i>=0;i--)
{
          for(int j=countArr.length-1;j>=0lj++)
          {
          }
}                                                                                     Takes 298 ms


Terminate loop by primitive


Primitive comparison is more efficient than function or variable comparison


Use temporary variables


for(int i=0; i
{
ar[0]+=i;
}                                                                  Takes   459 ms



tmp=0;
for(int i=0; i
{
tmp+=i;
}
ar[0]=tmp;                                                  Takes 189 ms








Take code out of loop       



Code that does not need to be executed on every pass
•Assignments
•Accesses
•Tests
•Method calls
•Method calls are costlier than the equivalent code without the method call


Put most common case first



•When several Boolean tests are made together in one expression in the loop,

•try to phrase the expression such that it  “short circuits” as soon as possible by putting the most likely case first

Switch vs. if-else

The average time of switch vs. if-else is about equall in random case




Recursive vs. tail-recursive





Dynamic cached recursive




No comments: