Re: Performance: switch vs if ... else if

2009-05-22 Thread Christopher Schultz
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

David,

On 5/21/2009 1:57 PM, David kerber wrote:
 Interesting.  From that description, depending on how sparse is
 sparse, there's probably a good chance I'm getting a tableswitch.

If you find that a tableswitch is /not/ being generated, you could add
dummy cases that don't do anything like this:

switch(comeChar) {
  case 'a': /* do something */
break;
  case 'b': /* do something */
break;
  case 'c': /* do NOTHING */
break;
  case 'd': /* do NOTHING */
break;
  case 'e': /* do something */
break;
}

You'll complicate your code a bit, but you'll also likely squeeze more
performance out of your code.

 Can you point me to a byte code interpreter so I could look at this?

What you really want is a bytecode disassembler (if that's even the
right term for this thing). I've used 'jad' in the past, but if you just
need to disassemble (instead of decompiling back to Java source),
Chuck's suggestion of using 'javap' is a good one: it's already included
with the JDK and works just fine.

- -chris
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkoWm/0ACgkQ9CaO5/Lv0PDo9QCfUWCJ4EfXgiorapcqxDKHReo0
G/MAnRIRBZXnclIbI+iT5gXsUeomB2IW
=TnmT
-END PGP SIGNATURE-

-
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org



RE: Performance: switch vs if ... else if

2009-05-22 Thread Caldarale, Charles R
 From: Christopher Schultz [mailto:ch...@christopherschultz.net]
 Subject: Re: Performance: switch vs if ... else if
 
 If you find that a tableswitch is /not/ being generated,

The tableswitch is being generated for the enum switch (rtFields), but not the 
char switch.  However, using the enum generates a ton of other invokes that 
will far outweigh any advantage of the tableswitch.  It's possible the JIT is 
optimizing much of that away, but it's hard to tell without seeing the 
generated native code (a tricky thing to dig out).

It's also possible the JIT could reorder the char switch into an indexed jump 
table even when javac doesn't, but again we'd need to see the native code to be 
sure. 

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY 
MATERIAL and is thus for use only by the intended recipient. If you received 
this in error, please contact the sender and delete the e-mail and its 
attachments from all computers.



Re: Performance: switch vs if ... else if

2009-05-22 Thread David kerber

Caldarale, Charles R wrote:

From: Christopher Schultz [mailto:ch...@christopherschultz.net]
Subject: Re: Performance: switch vs if ... else if

If you find that a tableswitch is /not/ being generated,



The tableswitch is being generated for the enum switch (rtFields), but not the 
char switch.  However, using the enum generates a ton of other invokes that 
will far outweigh any advantage of the tableswitch.  It's possible the JIT is 
optimizing much of that away, but it's hard to tell without seeing the 
generated native code (a tricky thing to dig out).

It's also possible the JIT could reorder the char switch into an indexed jump table even when javac doesn't, but again we'd need to see the native code to be sure. 
  
And probably not worth the effort right now unless somebody is just 
curious or doing some deep research.  I've already got an 
order-of-magnitude improvement in my implementation of this piece of 
code, and more than that on the servlet's overall performance.  As a 
result, right now I don't have any way of generating enough load to find 
a bottleneck in the overall servlet (which is a very good thing!!).  
I've got several machines sitting around that I'd probably have to 
configure as a client farm if I'm going to generate significant loads on 
this app now.


D



-
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org



RE: Performance: switch vs if ... else if

2009-05-22 Thread Peter Crowther
 From: David kerber [mailto:dcker...@verizon.net]
 As a
 result, right now I don't have any way of generating enough
 load to find
 a bottleneck in the overall servlet (which is a very good thing!!).

That's a win.  Congratulations!

- Peter

-
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org



Re: Performance: switch vs if ... else if

2009-05-21 Thread Christopher Schultz
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

David,

On 5/19/2009 3:04 PM, David kerber wrote:
 I have a section of code in a frequently-called (~3.5 million times
 per day) servlet where I had to process based on a parameter that
 could take one of 6 different single-character string values.  I had
 been using an if .. else if construct.  Then I discovered that java
 1.5 allowed constructing a switch on strings.  So I did some speed
 testing of a standalone java class that compared the 6-option switch
 that vs my 6-step if/elseif.

I'd be interested to see what the bytecode looks like. Javac compiles
switch statements (using primitives) to one of two bytecodes:
lookupswitch or tableswitch. If you can get the compiler to generate a
tableswitch, your performance will increase dramatically due to the way
the bytecode works.

See
http://java.sun.com/docs/books/jvms/second_edition/html/Compiling.doc.html#14942
for more information on these bytecodes.

- -chris
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkoVlE4ACgkQ9CaO5/Lv0PDzGgCeKdzJpFmzj8uBhaaC99IpSsky
6mQAoMRbUlgGymISSy9AZe79hstWMInR
=/Zfb
-END PGP SIGNATURE-

-
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org



Re: Performance: switch vs if ... else if

2009-05-21 Thread David kerber

Christopher Schultz wrote:

-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

David,

On 5/19/2009 3:04 PM, David kerber wrote:
  

I have a section of code in a frequently-called (~3.5 million times
per day) servlet where I had to process based on a parameter that
could take one of 6 different single-character string values.  I had
been using an if .. else if construct.  Then I discovered that java
1.5 allowed constructing a switch on strings.  So I did some speed
testing of a standalone java class that compared the 6-option switch
that vs my 6-step if/elseif.



I'd be interested to see what the bytecode looks like. Javac compiles
switch statements (using primitives) to one of two bytecodes:
lookupswitch or tableswitch. If you can get the compiler to generate a
tableswitch, your performance will increase dramatically due to the way
the bytecode works.

See
http://java.sun.com/docs/books/jvms/second_edition/html/Compiling.doc.html#14942
for more information on these bytecodes.
  
Interesting.  From that description, depending on how sparse is 
sparse, there's probably a good chance I'm getting a tableswitch.  Can 
you point me to a byte code interpreter so I could look at this?  Or I 
could just send you the .class file of my test class if you'd prefer.


D




-
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org



RE: Performance: switch vs if ... else if

2009-05-21 Thread Caldarale, Charles R
 From: David kerber [mailto:dcker...@verizon.net]
 Subject: Re: Performance: switch vs if ... else if
 
 Can you point me to a byte code interpreter so I could look at this?

The javap tool in the JDK will display the byte codes.  (I use it a lot.)  If 
you want, go ahead and send me the class file via private mail, since the list 
will likely strip attachments.

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY 
MATERIAL and is thus for use only by the intended recipient. If you received 
this in error, please contact the sender and delete the e-mail and its 
attachments from all computers.




Re: Performance: switch vs if ... else if

2009-05-20 Thread Ronald Klop

As with the tips about usage of char in the switch you can also test using a 
char in the if ... else.

if (myChr.length() != 1) {
  throw new RuntimeException(invalid length);
}
char ch = myChr.charAt(0);
if (ch == 'c') {
 p += 1;
} else if (ch == 'r') {
 p += 2;
} ...

Should make a difference in performance also.

And if you want to try more speed you can try to avoid the conditionals in the 
code.
private static final char OFFSET = 'a';
private static final int[] VALUES = init();

private static int[] init() {
  final int[] tmp = new int[26];
  tmp['c' - OFFSET] = 1;
  tmp['r' - OFFSET] = 2;
  tmp['l' - OFFSET] = 3;
  tmp['a' - OFFSET] = 4;
  tmp['t' - OFFSET] = 5;
  tmp['d' - OFFSET] = 6;
  return tmp;
}

private static int p = 0;
private static void ifArray(final String myChr) {
  final char ch = myChr.charAt(0);
  p += VALUES[ch - OFFSET];
}

Ronald.


Op dinsdag, 19 mei 2009 21:51 schreef David kerber  : 

Caldarale, Charles R wrote:
 From: David kerber [mailto:dcker...@verizon.net]
 Subject: Performance: switch vs if ... else if

 I had to process based on a parameter that could take
 one of 6 different single-character string values.  I
 had been using an if .. else if construct.
 


 Interesting numbers.  Can you show us the exact code of the if .. else 
construct?

  - Chuck
   


Here is the entire code.  The variables o and p are used to make sure there is 
actually some work done in each call to the test methods.  I've seen cases 
(mainly in Delphi) where an optimizer completely removed large chunks of code 
because it wasn't doing anything.  I don't know of java can do that or not...

public class SwitchVsIfTest {
static enumrtFields{ c, r, l, a, t, d }
static longn = 1;
static longo = 0;
static longp = 0;

public static void main( String[] args ) {
longt1;
longt2;
longt3;
longt4;

t1 = System.currentTimeMillis();
for ( int ii = 0; ii  n; ii++ ) {
switchSub( c );
switchSub( r );
switchSub( l );
switchSub( a );
switchSub( t );
switchSub( d );
}
t2 = System.currentTimeMillis();
System.out.println( Elapsed time for 'switch' version:  + ( t2 - t1 ) + , 
counter =  + o );

t3 = System.currentTimeMillis();
for ( int jj = 0; jj  n; jj++ ) {
ifSub( c );
ifSub( r );
ifSub( l );
ifSub( a );
ifSub( t );
ifSub( d );
}
t4 = System.currentTimeMillis();
System.out.println( Elapsed time for 'if' version:  + ( t4 - t3 ) + , 
counter =  + p );

t1 = System.currentTimeMillis();
for ( int ii = 0; ii  n; ii++ ) {
switchSub( c );
switchSub( r );
switchSub( l );
switchSub( a );
switchSub( t );
switchSub( d );
}
t2 = System.currentTimeMillis();
System.out.println( Elapsed time for 'switch' version:  + ( t2 - t1 ) + , 
counter =  + o );

t3 = System.currentTimeMillis();
for ( int jj = 0; jj  n; jj++ ) {
ifSub( c );
ifSub( r );
ifSub( l );
ifSub( a );
ifSub( t );
ifSub( d );
}
t4 = System.currentTimeMillis();
System.out.println( Elapsed time for 'if' version:  + ( t4 - t3 ) + , 
counter =  + p );

}
   
private static void switchSub( String myChr ) {

switch ( rtFields.valueOf( myChr )) {
case c:
o += 1;
break;
case r:
o += 2;
break;
case l:
o += 3;
break;
case a:
o += 4;
break;
case t:
o += 5;
break;
case d:
o += 6;
break;
}
return;
}
   
private static void ifSub( String myChr ) {

if ( myChr.equals( c )) {
p += 1;
} else if (myChr.equals( r )) {
p += 2;
} else if (myChr.equals( l )) {
p += 3;
} else if (myChr.equals( a )) {
p += 4;
} else if (myChr.equals( t )) {
p += 5;
} else if (myChr.equals( d )) {
p += 6;
}
}
}


D



-
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org

 







 

Re: Performance: switch vs if ... else if

2009-05-20 Thread David kerber

Ronald Klop wrote:
As with the tips about usage of char in the switch you can also test 
using a char in the if ... else.


if (myChr.length() != 1) {
  throw new RuntimeException(invalid length);
}
char ch = myChr.charAt(0);
if (ch == 'c') {
 p += 1;
} else if (ch == 'r') {
 p += 2;
} ...

Should make a difference in performance also.

Thanks for that suggestion.



And if you want to try more speed you can try to avoid the 
conditionals in the code.

private static final char OFFSET = 'a';
private static final int[] VALUES = init();

private static int[] init() {
  final int[] tmp = new int[26];
  tmp['c' - OFFSET] = 1;
  tmp['r' - OFFSET] = 2;
  tmp['l' - OFFSET] = 3;
  tmp['a' - OFFSET] = 4;
  tmp['t' - OFFSET] = 5;
  tmp['d' - OFFSET] = 6;
  return tmp;
}

private static int p = 0;
private static void ifArray(final String myChr) {
  final char ch = myChr.charAt(0);
  p += VALUES[ch - OFFSET];
}
This won't help in real life; the numeric incrementing in the demo code 
was just to be sure the optimizer didn't take too many shortcuts when I 
was speed testing.  The actual processing for each case in the real code 
is completely different.


D



-
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org



Performance: switch vs if ... else if

2009-05-19 Thread David kerber
This isn't directly related to tomcat, but does relate to the 
performance testing people have done over the last couple of days.


I have a section of code in a frequently-called (~3.5 million times per 
day) servlet where I had to process based on a parameter that could take 
one of 6 different single-character string values.  I had been using an 
if .. else if construct.  Then I discovered that java 1.5 allowed 
constructing a switch on strings.  So I did some speed testing of a 
standalone java class that compared the 6-option switch that vs my 
6-step if/elseif.


Under 1.5.0_12, Client vm, the switch was consistently faster, but by 
less than 1% (IOW, code readability was more important than the speed gain).


Under 1.6.0_13, Client vm, the if/elseif construct sped up by a factor 
of about 10% - 12%, and the switch only about 1%, so the if/elseif was 
now about 10% faster, probably worth doing depending on the usage.


Under 1.6.0_13, Server VM, the switch sped up by a factor of 4x from the 
1.6 Client run, and the if/elseif only slightly, so now the switch was 
about 1.8x faster, definitely worth doing.


I ran these tests several times, varying the order, and got pretty 
consistent results across multiple runs.  As with the testing I did on 
incrementing AtomicIntegers vs Synchronized counters, I was shocked at 
how much faster the server vm was than the client one.


Dave



-
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org



RE: Performance: switch vs if ... else if

2009-05-19 Thread Caldarale, Charles R
 From: David kerber [mailto:dcker...@verizon.net]
 Subject: Performance: switch vs if ... else if
 
 I had to process based on a parameter that could take
 one of 6 different single-character string values.  I
 had been using an if .. else if construct.

Interesting numbers.  Can you show us the exact code of the if .. else 
construct?

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY 
MATERIAL and is thus for use only by the intended recipient. If you received 
this in error, please contact the sender and delete the e-mail and its 
attachments from all computers.



-
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org



Re: Performance: switch vs if ... else if

2009-05-19 Thread David kerber

Caldarale, Charles R wrote:

From: David kerber [mailto:dcker...@verizon.net]
Subject: Performance: switch vs if ... else if

I had to process based on a parameter that could take
one of 6 different single-character string values.  I
had been using an if .. else if construct.



Interesting numbers.  Can you show us the exact code of the if .. else 
construct?

 - Chuck
  


Here is the entire code.  The variables o and p are used to make sure 
there is actually some work done in each call to the test methods.  I've 
seen cases (mainly in Delphi) where an optimizer completely removed 
large chunks of code because it wasn't doing anything.  I don't know of 
java can do that or not...


public class SwitchVsIfTest {
   static enumrtFields{ c, r, l, a, t, d }
   static longn = 1;
   static longo = 0;
   static longp = 0;

   public static void main( String[] args ) {
   longt1;
   longt2;
   longt3;
   longt4;

   t1 = System.currentTimeMillis();
   for ( int ii = 0; ii  n; ii++ ) {
   switchSub( c );
   switchSub( r );
   switchSub( l );
   switchSub( a );
   switchSub( t );
   switchSub( d );
   }
   t2 = System.currentTimeMillis();
   System.out.println( Elapsed time for 'switch' version:  + ( t2 
- t1 ) + , counter =  + o );


   t3 = System.currentTimeMillis();
   for ( int jj = 0; jj  n; jj++ ) {
   ifSub( c );
   ifSub( r );
   ifSub( l );
   ifSub( a );
   ifSub( t );
   ifSub( d );
   }
   t4 = System.currentTimeMillis();
   System.out.println( Elapsed time for 'if' version:  + ( t4 - 
t3 ) + , counter =  + p );


   t1 = System.currentTimeMillis();
   for ( int ii = 0; ii  n; ii++ ) {
   switchSub( c );
   switchSub( r );
   switchSub( l );
   switchSub( a );
   switchSub( t );
   switchSub( d );
   }
   t2 = System.currentTimeMillis();
   System.out.println( Elapsed time for 'switch' version:  + ( t2 
- t1 ) + , counter =  + o );


   t3 = System.currentTimeMillis();
   for ( int jj = 0; jj  n; jj++ ) {
   ifSub( c );
   ifSub( r );
   ifSub( l );
   ifSub( a );
   ifSub( t );
   ifSub( d );
   }
   t4 = System.currentTimeMillis();
   System.out.println( Elapsed time for 'if' version:  + ( t4 - 
t3 ) + , counter =  + p );


   }
  
   private static void switchSub( String myChr ) {

   switch ( rtFields.valueOf( myChr )) {
   case c:
   o += 1;
   break;
   case r:
   o += 2;
   break;
   case l:
   o += 3;
   break;
   case a:
   o += 4;
   break;
   case t:
   o += 5;
   break;
   case d:
   o += 6;
   break;
   }
   return;
   }
  
   private static void ifSub( String myChr ) {

   if ( myChr.equals( c )) {
   p += 1;
   } else if (myChr.equals( r )) {
   p += 2;
   } else if (myChr.equals( l )) {
   p += 3;
   } else if (myChr.equals( a )) {
   p += 4;
   } else if (myChr.equals( t )) {
   p += 5;
   } else if (myChr.equals( d )) {
   p += 6;
   }
   }
}


D



-
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org



Re: Performance: switch vs if ... else if

2009-05-19 Thread George Sexton
If you're only doing a single character, you would probably get better 
performance with:


switch (sString.char(0)) {
   case 'A':
   case 'B':
   case 'C':
}

David kerber wrote:

Caldarale, Charles R wrote:

From: David kerber [mailto:dcker...@verizon.net]
Subject: Performance: switch vs if ... else if

I had to process based on a parameter that could take
one of 6 different single-character string values.  I
had been using an if .. else if construct.



Interesting numbers.  Can you show us the exact code of the if .. 
else construct?


 - Chuck
  


Here is the entire code.  The variables o and p are used to make sure 
there is actually some work done in each call to the test methods.  
I've seen cases (mainly in Delphi) where an optimizer completely 
removed large chunks of code because it wasn't doing anything.  I 
don't know of java can do that or not...


public class SwitchVsIfTest {
   static enumrtFields{ c, r, l, a, t, d }
   static longn = 1;
   static longo = 0;
   static longp = 0;

   public static void main( String[] args ) {
   longt1;
   longt2;
   longt3;
   longt4;

   t1 = System.currentTimeMillis();
   for ( int ii = 0; ii  n; ii++ ) {
   switchSub( c );
   switchSub( r );
   switchSub( l );
   switchSub( a );
   switchSub( t );
   switchSub( d );
   }
   t2 = System.currentTimeMillis();
   System.out.println( Elapsed time for 'switch' version:  + ( 
t2 - t1 ) + , counter =  + o );


   t3 = System.currentTimeMillis();
   for ( int jj = 0; jj  n; jj++ ) {
   ifSub( c );
   ifSub( r );
   ifSub( l );
   ifSub( a );
   ifSub( t );
   ifSub( d );
   }
   t4 = System.currentTimeMillis();
   System.out.println( Elapsed time for 'if' version:  + ( t4 - 
t3 ) + , counter =  + p );


   t1 = System.currentTimeMillis();
   for ( int ii = 0; ii  n; ii++ ) {
   switchSub( c );
   switchSub( r );
   switchSub( l );
   switchSub( a );
   switchSub( t );
   switchSub( d );
   }
   t2 = System.currentTimeMillis();
   System.out.println( Elapsed time for 'switch' version:  + ( 
t2 - t1 ) + , counter =  + o );


   t3 = System.currentTimeMillis();
   for ( int jj = 0; jj  n; jj++ ) {
   ifSub( c );
   ifSub( r );
   ifSub( l );
   ifSub( a );
   ifSub( t );
   ifSub( d );
   }
   t4 = System.currentTimeMillis();
   System.out.println( Elapsed time for 'if' version:  + ( t4 - 
t3 ) + , counter =  + p );


   }
 private static void switchSub( String myChr ) {
   switch ( rtFields.valueOf( myChr )) {
   case c:
   o += 1;
   break;
   case r:
   o += 2;
   break;
   case l:
   o += 3;
   break;
   case a:
   o += 4;
   break;
   case t:
   o += 5;
   break;
   case d:
   o += 6;
   break;
   }
   return;
   }
 private static void ifSub( String myChr ) {
   if ( myChr.equals( c )) {
   p += 1;
   } else if (myChr.equals( r )) {
   p += 2;
   } else if (myChr.equals( l )) {
   p += 3;
   } else if (myChr.equals( a )) {
   p += 4;
   } else if (myChr.equals( t )) {
   p += 5;
   } else if (myChr.equals( d )) {
   p += 6;
   }
   }
}


D



-
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org



--
George Sexton
MH Software, Inc.
Voice: +1 303 438 9585
URL:   http://www.mhsoftware.com/


-
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org



Re: Performance: switch vs if ... else if

2009-05-19 Thread Konstantin Kolinko
2009/5/19 David kerber dcker...@verizon.net:
 Caldarale, Charles R wrote:

 From: David kerber [mailto:dcker...@verizon.net]
 Subject: Performance: switch vs if ... else if

 I had to process based on a parameter that could take
 one of 6 different single-character string values.  I
 had been using an if .. else if construct.


 Interesting numbers.  Can you show us the exact code of the if .. else
 construct?

  - Chuck


 Here is the entire code.  The variables o and p are used to make sure there
 is actually some work done in each call to the test methods.  I've seen
 cases (mainly in Delphi) where an optimizer completely removed large chunks
 of code because it wasn't doing anything.  I don't know of java can do that
 or not...

 public class SwitchVsIfTest {
   (...)


1. If you are doing tests with the classic VM, allow it some time to warmup and
compile your code. That is, run the same test first with a smaller count of
iterations.

Server VM precompiles code before using it, while Classic one compiles
heavily used parts of code on-the-fly.

2. This line:
rtFields.valueOf( myChr )
does all the job that your if/elif/else tree did. That is, I expect
that all the time
is spent in there, not in switch(number) that follows it.

3. You can try the following:

private static void switchSub2( String myChr ) {
  if (myChr.length() != 1) {
  throw new AssertionError();
  }
  switch ( myChr.charAt(0)) {
  case 'c':
  o += 1;
  break;
  case 'r':
  o += 2;
  break;
  case 'l':
  o += 3;
  break;
  case 'a':
  o += 4;
  break;
  case 't':
  o += 5;
  break;
  case 'd':
  o += 6;
  break;
  }
  return;
  }

Best regards,
Konstantin Kolinko

-
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org



Re: Performance: switch vs if ... else if

2009-05-19 Thread David kerber

Konstantin Kolinko wrote:

2009/5/19 David kerber dcker...@verizon.net:
  

Caldarale, Charles R wrote:


From: David kerber [mailto:dcker...@verizon.net]
Subject: Performance: switch vs if ... else if

I had to process based on a parameter that could take
one of 6 different single-character string values.  I
had been using an if .. else if construct.



Interesting numbers.  Can you show us the exact code of the if .. else
construct?

 - Chuck

  

Here is the entire code.  The variables o and p are used to make sure there
is actually some work done in each call to the test methods.  I've seen
cases (mainly in Delphi) where an optimizer completely removed large chunks
of code because it wasn't doing anything.  I don't know of java can do that
or not...

public class SwitchVsIfTest {
  (...)




1. If you are doing tests with the classic VM, allow it some time to warmup and
compile your code. That is, run the same test first with a smaller count of
iterations.

Server VM precompiles code before using it, while Classic one compiles
heavily used parts of code on-the-fly.
  
That's part of the reason I ran the tests twice in a row, in the main() 
method (that, and cache warming).


D



-
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org



Re: Performance: switch vs if ... else if

2009-05-19 Thread David kerber

Konstantin Kolinko wrote:
...


1. If you are doing tests with the classic VM, allow it some time to warmup and
compile your code. That is, run the same test first with a smaller count of
iterations.

Server VM precompiles code before using it, while Classic one compiles
heavily used parts of code on-the-fly.

2. This line:
rtFields.valueOf( myChr )
does all the job that your if/elif/else tree did. That is, I expect
that all the time
is spent in there, not in switch(number) that follows it.

3. You can try the following:

private static void switchSub2( String myChr ) {
  if (myChr.length() != 1) {
  throw new AssertionError();
  }
  switch ( myChr.charAt(0)) {
  case 'c':
  o += 1;
  break;
  case 'r':
  o += 2;
  break;
  case 'l':
  o += 3;
  break;
  case 'a':
  o += 4;
  break;
  case 't':
  o += 5;
  break;
  case 'd':
  o += 6;
  break;
  }
  return;
  }
  

You're right; it's dramatically faster, ~2.5x!!

Thanks!
D



-
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org



RE: Performance: switch vs if ... else if

2009-05-19 Thread Caldarale, Charles R
 From: Konstantin Kolinko [mailto:knst.koli...@gmail.com]
 Subject: Re: Performance: switch vs if ... else if
 
 Server VM precompiles code before using it, while Classic one compiles
 heavily used parts of code on-the-fly.

Your terminology is incorrect, as are your descriptions of when code is 
compiled.  The phrase Classic VM refers to pre-HotSpot VMs - 1.3 and prior 
levels.  The Classic VM compiled byte codes into native instructions at first 
reference, and only did that once per method.  The two JITs in the HotSpot VM 
both wait to see how frequently code is used before deciding to compile it or 
not, and look at usage patterns to see where to optimize (hence the name 
HotSpot).  The client mode JIT (known as C1) starts compiling much earlier 
than the server mode JIT (C2) and is a much faster compiler, but does not 
optimize anywhere near as much as C2 does.  Both the C1 and C2 JITs have the 
capability of recompiling code when needed to improve optimization (primarily 
by doing additional method inlining).

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY 
MATERIAL and is thus for use only by the intended recipient. If you received 
this in error, please contact the sender and delete the e-mail and its 
attachments from all computers.


-
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org



Re: Performance: switch vs if ... else if

2009-05-19 Thread Konstantin Kolinko
2009/5/20 Caldarale, Charles R chuck.caldar...@unisys.com:
 From: Konstantin Kolinko [mailto:knst.koli...@gmail.com]
 Subject: Re: Performance: switch vs if ... else if

 Server VM precompiles code before using it, while Classic one compiles
 heavily used parts of code on-the-fly.

 Your terminology is incorrect, as are your descriptions of when code is 
 compiled.  The phrase Classic VM refers to pre-HotSpot VMs - 1.3 and prior 
 levels.
 (...)

Thank you very much for correcting me.

I surely meant Client VM there, it was just a misprint.

Regarding Server VM: I accept it as my mistake. I know, that Client
VM is optimized for faster startup (and, well, it is written in the
FAQ [1]), but I do not know why I thought that slower startup of
Server VM is due to precompilations. Thank you for correcting me.

[1] http://java.sun.com/docs/hotspot/HotSpotFAQ.html#compiler_types

2009/5/20 David kerber dcker...@verizon.net:

 That's part of the reason I ran the tests twice in a row, in the main()
 method (that, and cache warming).


Oh, I see. I missed that.

Best regards,
Konstantin Kolinko

-
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org



RE: Performance: switch vs if ... else if

2009-05-19 Thread Caldarale, Charles R
 From: Konstantin Kolinko [mailto:knst.koli...@gmail.com]
 Subject: Re: Performance: switch vs if ... else if
 
 Regarding Server VM: I accept it as my mistake. I know, that Client
 VM is optimized for faster startup (and, well, it is written in the
 FAQ [1]), but I do not know why I thought that slower startup of
 Server VM is due to precompilations.

Both the Client and Server VMs use an interpreter to get things going, which is 
why the -version option displays mixed mode in the signon line.  Both JITs 
compile in the background while methods execute under the interpreter; the 
client JIT just gets started sooner.  You can disable the JIT in either the 
Client or the Server VM with the -Xint option - and things will run really 
slowly, but you can single-step through the byte codes this way.

Sometime in the future, we'll see the client and server JITs in the same VM, 
used as a two-stage compilation mechanism (referred to as tiered 
compilation).  This is at least partially implemented in 1.6; don't know if it 
will be enabled in the final release of 1.7 or deferred until a later level 
(one of the lead guys working on it died of an aneurism last year).

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY 
MATERIAL and is thus for use only by the intended recipient. If you received 
this in error, please contact the sender and delete the e-mail and its 
attachments from all computers.


-
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org