Wednesday, December 2, 2009

Simple Optimization Functions.

Here are some functions I came up with for various purposes, I'm sure there are more efficient versions out there. If you have any ideas on how to optimize these functions more please comment. If anyone has the time to test these out against their Math.function versions, please let me know the results. Thanks ahead of time.

Faster Rounding (Needs to be tested):
function round (ax:Number):int
{
if (ax>=0){
return int(ax+.5)
} else {
return int(ax-.5)
}
}

Faster Ceiling (Needs to be tested):
function ceil (ax:Number):int
{
if (ax>0){
return int(ax+1)
} else {
return int(ax)
}
}

Faster Flooring (Needs to be tested):
function floor (ax:Number):int
{
if (ax>=0){
return int(ax)
} else {
return int(ax-1)
}
}


Faster Absolute Value (Needs to be tested):
function abs (ax:Number):Number{
if (ax>=0){
return ax
} else {
return -ax
}
}


3 comments:

  1. this setup will work for all your functions i think. just change the apropriate value..

    function round (ax:Number):int
    {
    return (ax>=0)?int(ax+.5):int(ax-.5)
    }

    ReplyDelete
  2.      Those are going to fail at certain boundary conditions. (I am assuming the same truncation rules as C, C++, Java, and Javascript.) For example, when computing floor(-1), ax >= 0 will be false, and it will return int(-2) = -2.
         BTW I prefer to avoid using the names of processor registers (ax, bx, cx, dx, sp, bp, si, di) as variable names. I find it difficult to think of them as other than the familiar 16-bit registers.

    ReplyDelete
  3. Best friv and flash games in this site. www.frivscore.com

    ReplyDelete