In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

Performance tests if vs switch

Get Adobe Flash player
by leichtgewicht 11 Sep 2011
    Embed
/**
 * Copyright leichtgewicht ( http://wonderfl.net/user/leichtgewicht )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/tqsO
 */

package {
    import flash.display.Sprite;
    import flash.utils.getTimer;
    public class FlashTest extends Sprite {
        public function FlashTest() {
            addChild(TRACE);
            TRACE.width = stage.stageWidth;
            TRACE.height = stage.stageHeight;
            
            const amount: int = 100000;
            var t: Number;
            var i: int;
            
            t = getTimer();
            i = amount;
            while( --i ){
                switchStrings("a");
                switchStrings("b");
                switchStrings("c");
                switchStrings("d");
                switchStrings("e");
            }
            trace(getTimer()-t + "ms for switchStrings");

            t = getTimer();
            i = amount;
            while( --i ){
                ifStrings("a");
                ifStrings("b");
                ifStrings("c");
                ifStrings("d");
                ifStrings("e");
            }
            trace(getTimer()-t + "ms for ifStrings");
            
            t = getTimer();
            i = amount;
            while( --i ){
                switchStringWithVar("a", "a", "b", "c","d");
                switchStringWithVar("b", "a", "b", "c","d");
                switchStringWithVar("c", "a", "b", "c","d");
                switchStringWithVar("d", "a", "b", "c","d");
                switchStringWithVar("e", "a", "b", "c","d");
            }
            trace(getTimer()-t + "ms for switchStringsWithVar");
            
            t = getTimer();
            i = amount;
            while( --i ){
                ifStringsWithVar("a", "a", "b", "c","d");
                ifStringsWithVar("b", "a", "b", "c","d");
                ifStringsWithVar("c", "a", "b", "c","d");
                ifStringsWithVar("d", "a", "b", "c","d");
                ifStringsWithVar("e", "a", "b", "c","d");
            }
            trace(getTimer()-t + "ms for ifStringsWithVar");
            
            t = getTimer();
            i = amount;
            while( --i ){
                switchInt(1);
                switchInt(2);
                switchInt(3);
                switchInt(4);
                switchInt(5);
            }
            trace(getTimer()-t + "ms for switchInt");
            
            t = getTimer();
            i = amount;
            while( --i ){
                ifInt(1);
                ifInt(2);
                ifInt(3);
                ifInt(4);
                ifInt(5);
            }
            trace(getTimer()-t + "ms for ifInt");
            
            const u1:uint = 1;
            const u2:uint = 2;
            const u3:uint = 3;
            const u4:uint = 4;
            const u5:uint = 5;
            t = getTimer();
            i = amount;
            while( --i ){
                switchUInt(u1);
                switchUInt(u2);
                switchUInt(u3);
                switchUInt(u4);
                switchUInt(u5);
            }
            trace(getTimer()-t + "ms for switchUInt");
            
            t = getTimer();
            i = amount;
            while( --i ){
                ifUInt(u1);
                ifUInt(u2);
                ifUInt(u3);
                ifUInt(u4);
                ifUInt(u5);
            }
            trace(getTimer()-t + "ms for ifUInt");
        }

        
        public function switchStrings(str:String):void {
            switch( str ) {
               case "a":
                   str;
                   break;
               case "b":
                   str;
                   break;
               case "c":
                   str;
                   break;
               case "d":
                   str;
                   break;
               default:
                   str;
                   break;
            }
        }
        
        public function ifStrings(str:String):void {
            if( str == "a" ) { str;
            } else if( str == "b" ) { str;
            } else if( str == "c" ) { str;
            } else if( str == "d" ) { str;
            } else { str;
            }
        }
        
        public function switchStringWithVar(str: String, varA: String, varB: String, varC: String, varD: String):void {
            switch( str ) {
                case varA:
                   str;
                   break;
                case varB:
                   str;
                   break;
                case varC:
                   str;
                   break;
                case varD:
                   str;
                   break;
                default:
                   str;
                   break;
            }

        }
        
        public function ifStringsWithVar(str:String, varA: String, varB: String, varC: String, varD: String):void {
            if( str == varA ) { str;
            } else if( str == varB ) { str;
            } else if( str == varC ) { str;
            } else if( str == varD ) { str;
            } else { str;
            }
        }

        
        public function switchInt(i:int):void {
            switch( i ) {
               case 1:
                   i;
                   break;
               case 2:
                   i;
                   break;
               case 3:
                   i;
                   break;
               case 4:
                   i;
                   break;
               default:
                   i;
                   break;
            }
        }
        
        public function ifInt(i:int):void {
            if( i == 1 ) { i;
            } else if( i == 2 ) { i;
            } else if( i == 3 ) { i;
            } else if( i == 4 ) { i;
            } else { i;
            }
        }

        public function switchUInt(i:uint):void {
            switch( i ) {
               case 1:
                   i;
                   break;
               case 2:
                   i;
                   break;
               case 3:
                   i;
                   break;
               case 4:
                   i;
                   break;
               default:
                   i;
                   break;
            }
        }
        
        public function ifUInt(i:uint):void {
            if( i == 1 ) { i;
            } else if( i == 2 ) { i;
            } else if( i == 3 ) { i;
            } else if( i == 4 ) { i;
            } else { i;
            }
        }
    }
    
}
import flash.text.TextField;

const TRACE: TextField = new TextField();
function trace(...args:Array):void {
    TRACE.appendText(args.join(" ")+"\n");
}