Wednesday, March 7, 2012

Accessing Properties is SLOW.

I was having a really tough time making the trivial decision on how to store
an array of point data. My "Ben-Day-Dots" effects sometimes lagged, so I was
thinking of some optimizations I could do to it to make it faster.
I am working on an graphic equalizer that will also use point data,
so I wanted to not make the same mistake I made in my last effect.

Output:
tt==100___METHOD ONE(1)
tt==58___METHOD TWO(2)
tt==43___METHOD THREE(3)


    //timer vars:
    var tt:int = 0;
    var t1:int = 0;
    var t2:int = 0;

    //timer loop vars:
    var ii:int = 0;
    var MX:int = 6543; //max iterations.

    //What is faster... A vector of points, or a vector of Numbers?
    var pVec:Vector.<Point> = new Vector.<Point>(255);
    var nv_X:Vector.<Number> = new Vector.<Number>(255);
    var nv_Y:Vector.<Number> = new Vector.<Number>(255);
    var xx:int = 0;
    var pVar:Point;
    var xNum:Number;
    var yNum:Number;

    //Populate vectors with same numbers:
    for(xx = 0; xx<256; xx++){
        pVec[xx] = new Point(xx,xx);
        nv_X[xx] = xx;
        nv_Y[xx] = xx;
    }

    //access method#1
    t1 = getTimer();
    for(ii=0; ii<MX; ii++){
        //access method#1
        for(xx = 0; xx<256; xx++){
            xNum = pVec[xx].x;
            yNum = pVec[xx].y;
        }
    }
    t2 = getTimer();
    tt = t2-t1;
    trace("tt==" + tt + "___METHOD ONE(1)");

    //access method#2
    t1 = getTimer();
    for(ii=0; ii<MX; ii++){
        //access method#2
        for(xx = 0; xx<256; xx++){
            pVar = pVec[xx];
            xNum = pVar.x;
            yNum = pVar.y;
        }
    }
    t2 = getTimer();
    tt = t2-t1;
    trace("tt==" + tt + "___METHOD TWO(2)");

    //access method#3
    t1 = getTimer();
    for(ii=0; ii<MX; ii++){
        //access method#3
        for(xx = 0; xx<256; xx++){
            xNum = nv_X[xx];
            yNum = nv_Y[xx];
        }
    }
    t2 = getTimer();
    tt = t2-t1;
    trace("tt==" + tt + "___METHOD THREE(3)");

No comments:

Post a Comment