tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From David kerber <>
Subject Performance: switch vs if ... else if
Date Tue, 19 May 2009 19:04:23 GMT
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.


To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message