_flash_benchmarks

-swf9 microbench.swf
-swf-header 680:480:60:FFFFFE 
-swf-version 10 
--flash-strict 
-main Microbench


class TNode
{
    public var next : TNode;
    public function new() {}
}

class TNode2 implements Dynamic
{
    //public var next : TNode2;
    public function new() {}
}

typedef TNode3 = {next : TNode3};

enum EnumTest {
    None;
    Intvar(foo : Int);
    Floatvar(foo : Float);
}

class FastFloor
{
    public static inline function fastFloor(f : Float)
    {
        
        var accum : Int = 0;
            
            
        if (f>0)
        {
            while (f>=32768) { accum+=32768; f-=32768; }
            if (f>=16384) { accum+=16384; f-=16384; }
            if (f>=8192) { accum+=8192; f-=8192; }
            if (f>=4096) { accum+=4096; f-=4096; }
            if (f>=2048) { accum+=2048; f-=2048; }
            if (f>=1024) { accum+=1024; f-=1024; }
            if (f>=512) { accum+=512; f-=512; }
            if (f>=256) { accum+=256; f-=256; }
            if (f>=128) { accum+=128; f-=128; }
            if (f>=64) { accum+=64; f-=64; }
            if (f>=32) { accum+=32; f-=32; }
            if (f>=16) { accum+=16; f-=16; }
            if (f>=8) { accum+=8; f-=8; }
            if (f>=4) { accum+=4; f-=4; }
            if (f>=2) { accum+=2; f-=2; }
            if (f>=1) { accum++; f--; }            
            return accum;
        }
        else
        {
            while (f<=-32768) { accum-=32768; f+=32768; }
            if (f<=-16384) { accum-=16384; f+=16384; }
            if (f<=-8192) { accum-=8192; f+=8192; }
            if (f<=-4096) { accum-=4096; f+=4096; }
            if (f<=-2048) { accum-=2048; f+=2048; }
            if (f<=-1024) { accum-=1024; f+=1024; }
            if (f<=-512) { accum-=512; f+=512; }
            if (f<=-256) { accum-=256; f+=256; }
            if (f<=-128) { accum-=128; f+=128; }
            if (f<=-64) { accum-=64; f+=64; }
            if (f<=-32) { accum-=32; f+=32; }
            if (f<=-16) { accum-=16; f+=16; }
            if (f<=-8) { accum-=8; f+=8; }
            if (f<=-4) { accum-=4; f+=4; }
            if (f<=-2) { accum-=2; f+=2; }
            if (f<=-1) { accum--; f++; }            
            return accum;            
        }
    }
}

class IntDivision
{
    inline static function abs(x : Int) {return ((x) < 0 ? ~(x)+1 : (x));}

    public static inline function posIntDiv(num : Int, denom : Int)
    {
      var a=0;
      var b=0;
      var i= 31; // CAREFUL: works only on int=32-bit machine!
      /* Work from leftmost to rightmost bit in numerator */
      while(i>=0) {
        /* appends one bit from numerator to a */
        a = (a << 1) + ((num & (1 << i)) >> i);
        b = b << 1;
        if (a >= denom) {
          a -= denom;
          b++;
        }
      i--;
      }
      return b;
    }
    
    public static inline function intDiv(a : Int, b : Int)
    {
        var dividend = abs(a);
        var divisor = abs(b);
        
        if (divisor==0) return 0;
        
        else if (divisor > dividend) 
        {
            return 0;
        }

        else if (divisor == dividend) 
        {
            return 1;
        }
        
        else
        {
        
            var quotient = posIntDiv(dividend,divisor);
            
            if (a<0) { if (b>0) return ~quotient+1; else return quotient; }
            else { if (b<0) return ~quotient+1; else return quotient; }
        }
    }
}

class Microbench
{

    public var m : Float;
    public var n : Float;
    
    public static inline var ITERCOUNT = 200000;

    public inline function enumtest()
    {
        var z = 0;
        var tm : EnumTest = None;
        while (z<ITERCOUNT) 
        {
            switch(tm)
            {
                case None:
                    tm = Intvar(10);
                case Intvar(foo):
                    tm = Floatvar(10.);
                case Floatvar(foo):
                    tm = None;
            }
            z++; 
        }        
    }

    public inline function typedtraversal()
    {
        var tnode = new TNode();
        var tnode2 = new TNode();
        tnode.next = tnode2;
        tnode2.next = tnode;
        var z = 0;
        var ptr : TNode = tnode;
        while (z<ITERCOUNT) { ptr = ptr.next; z++; }    
    }

    public inline function typedtraversal2()
    {
        var tnode = new TNode2();
        var tnode2 = new TNode2();
        tnode.next = tnode2;
        tnode2.next = tnode;
        var z = 0;
        var ptr : TNode2 = tnode;
        while (z<ITERCOUNT) { ptr = ptr.next; z++; }    
    }
    
    public inline function typedtraversal3()
    {
        var tnode : TNode3 = {next : null};
        var tnode2 : TNode3 = {next : null};
        tnode.next = tnode2;
        tnode2.next = tnode;
        var z = 0;
        var ptr : TNode3 = tnode;
        while (z<ITERCOUNT) { ptr = ptr.next; z++; }            
    }

    public inline function dynamictraversal()
    {
        var tnode : Dynamic = new TNode();
        var tnode2 : Dynamic = new TNode();
        tnode.next = tnode2;
        tnode2.next = tnode;
        var z = 0;
        var ptr : Dynamic = tnode;
        while (z<ITERCOUNT) { ptr = ptr.next; z++; }    
    }

    public inline function dynamictraversal2()
    {
        var tnode : Dynamic = {};
        var tnode2 : Dynamic = {};
        tnode.next = tnode2;
        tnode2.next = tnode;
        var z = 0;
        var ptr : Dynamic = tnode;
        while (z<ITERCOUNT) { ptr = ptr.next; z++; }    
    }
    
    public inline function reflecttraversal()
    {
        var tnode = new TNode();
        var tnode2 = new TNode();
        tnode.next = tnode2;
        tnode2.next = tnode;
        var z = 0;
        var ptr : TNode = tnode;
        while (z<ITERCOUNT) { ptr = Reflect.field(ptr,"next"); z++; }        
    }

    public inline function reflecttraversal2()
    {
        var tnode : Dynamic = new TNode();
        var tnode2 : Dynamic = new TNode();
        tnode.next = tnode2;
        tnode2.next = tnode;
        var z = 0;
        var ptr : Dynamic = tnode;
        while (z<ITERCOUNT) { ptr = Reflect.field(ptr,"next"); z++; }        
    }

    public inline function whileloop()
    {
        var z = 0;
        while (z<ITERCOUNT)
        {
            z++;
        }
    }

    public inline function compare()
    {
        var z = 0;
        while (z<ITERCOUNT)
        {
            if (m>n)
                {}
            else
                {}
            z++;
        }
    }
    
    public inline function dividefloor()
    {
        var z = 0;
        while (z<ITERCOUNT)
        {
            n = Std.int(m/z);
            z++;
        }
    }
    
    public inline function intdivide()
    {
        var z = 0;
        var mm = Std.int(m);
        while (z<ITERCOUNT)
        {
            IntDivision.intDiv(mm,z);
            z++;
        }
    }

    public inline function dividefastfloor()
    {
        var z = 1;
        while (z<ITERCOUNT+1)
        {
            n = FastFloor.fastFloor(m/z);
            z++;
        }
    }
    
    public inline function divide()
    {
        var z = 0;
        while (z<ITERCOUNT)
        {
            n = m/z;
            z++;
        }
    }
    
    public inline function multiply()
    {
        var z = 0;
        while (z<ITERCOUNT)
        {
            n = m*z;
            z++;
        }    
    }

    public inline function add()
    {
        var z = 0;
        while (z<ITERCOUNT)
        {
            n = m+z;
            z++;
        }
    }

    public inline function subtract()
    {
        var z = 0;
        while (z<ITERCOUNT)
        {
            n = m-z;
            z++;
        }
    }

    public function randomize()
    {
        m = 1.+Math.random()*100;
        n = 1.+Math.random()*100;        
    }

    public var ihash : Hash<Int>;

    public function doIter(iterfunc : Void->Void, itername : String)
    {
        var t0 = flash.Lib.getTimer();
        randomize();
        iterfunc();
        var t1 = flash.Lib.getTimer();
        
        if (ihash.exists(itername))
            ihash.set(itername,t1-t0+ihash.get(itername));
        else
            ihash.set(itername,t1-t0);
    }

    public function runBench(?e : Dynamic)
    {
        ihash = new Hash();
        
        var count = 0;
        while (count<10)
        {
            doIter(enumtest,"enumtest");
            doIter(typedtraversal,"typedtraversal");
            doIter(typedtraversal2,"typedtraversal2");
            doIter(typedtraversal3,"typedtraversal3");
            doIter(dynamictraversal,"dynamictraversal");
            doIter(dynamictraversal2,"dynamictraversal2");
            doIter(reflecttraversal,"reflecttraversal");
            doIter(reflecttraversal2,"reflecttraversal2");
            doIter(whileloop,"whileloop");
            doIter(compare,"compare");
            doIter(divide,"divide");
            doIter(intdivide,"intdivide");
            doIter(dividefloor,"dividefloor");
            doIter(dividefastfloor,"dividefastfloor");
            doIter(multiply,"multiply");
            doIter(add,"add");
            doIter(subtract,"subtract");
            count++;
        }
        
        for (k in ihash.keys())
        {
            trace([k,ihash.get(k)]);
        }
        
    }

    public function new()
    {
        flash.Lib.current.stage.addEventListener(flash.events.MouseEvent.CLICK, runBench);        
        trace("click to start");
    }
    
    public static var inst : Microbench;

    public static function main()
    {
        inst = new Microbench();
    }


}
version #8381, modified 2010-04-02 07:17:04 by rtf