A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: Optimization & Math Function call

  1. #1

    Optimization & Math Function call

    I'm trying to optimize my project and eliminate unnecessary function calls, and the one I use the most is Math.abs. I ran a quick test and I use it 200-300 times each frame; checking for collision between the character and monsters, determining if objects are close enough to be in view, and some other misc calculations of that sort.

    My question is: am I using it enough that optimizing for it will see any real performance boost? If so, how else can I possibly express Math.abs?

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Are you using "Math.abs" in every call? If so, then that is TWO things and you can eliminate one of those. The first is looking in the Math object to find what abs is, the second is calling the abs function. You can save a local reference to Math.abs in a variable, and eliminate the lookup.
    Code:
    var abs:Function = Math.abs;
    
    var num:Number = abs(someOtherVariable);
    Put the local abs in a scope where it's visible to a lot of your functions, probably as an instance level private property.

    This sort of optimization is good practice since it's easy and pure win. Beware of premature optimization in general though, since it may have you spend a lot of effort for little gain, or even make things hard to understand for little benefit.

  3. #3
    Member
    Join Date
    Dec 2009
    Location
    UK
    Posts
    35
    Hmmmm.

    Actionscript Code:
    var t:int = getTimer();
    var n:Number;
    var num:Number = -1.23456789;

    for (var i:uint=0;i<100000;i++){
        n = Math.abs(num);
    }
    trace(getTimer()-t);

    var abs:Function = Math.abs;

    t = getTimer();
    for (i=0;i<100000;i++){
        n = abs(num);
    }
    trace(getTimer()-t);

    function getAbs(val:Number):Number{
        return (val<0)?-val:val;
    }

    t = getTimer();
    for (i=0;i<100000;i++){
        n = getAbs(num);
    }
    trace(getTimer()-t);

    First trace gives around 15-17ms, second gives 37-39ms, third trace is in the 12-14ms range, consistently quicker than Math.abs.

    Flash is odd

    Cheers,
    Rob

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I understand the locally defined getAbs being quicker, but how on earth can the second case (the one I recommended) be slower? It's doing less!
    Flash is weird.

  5. #5
    Member
    Join Date
    Dec 2009
    Location
    UK
    Posts
    35
    I should've added that was CS3 to FP9 so maybe things have improved?

  6. #6
    Member
    Join Date
    Dec 2009
    Location
    UK
    Posts
    35
    In answer to the original question, no you're probably not doing enough calls to Math.abs to make any noticeable different. Changing the loop value in the above tests to 300 sees traces in the 0-1ms range for all three methods. I have found the quickest though, and that's to get rid of all function calls completely:

    Actionscript Code:
    for (i=0;i<100000;i++){
        n = (num>0)?num:-num;
    }

    That's giving me a trace time of 3ms.

    Cheers,
    Rob

  7. #7
    I think I'll play with these and see which gives me the best performance result without making my work completely illegible.

    While poking around online I was extremely surprised by the results of a similar test with java here: http://jsperf.com/absolute-value-speed/3 I have no idea if flash behaves the same way, but to see such a huge difference between the different browsers really threw me for a loop.

  8. #8
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    As the domain name implies, jsperf is about javascript, not java.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center