tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From George Sexton <geor...@mhsoftware.com>
Subject Re: Performance: switch vs if ... else if
Date Tue, 19 May 2009 20:21:22 GMT
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:dckerber@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 enum    rtFields{ c, r, l, a, t, d }
>    static long    n = 100000000;
>    static long    o = 0;
>    static long    p = 0;
>
>    public static void main( String[] args ) {
>        long    t1;
>        long    t2;
>        long    t3;
>        long    t4;
>
>        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-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>

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


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


Mime
View raw message