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 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-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org





 

Reply via email to