The Great Code Optimization Thread (was "Round a number???")

149 posts (showing 1-20)
berto

Market Level 0Community Level 4
323 posts

Ok i have this 1.0387485749769 and i want to get this 1

 

So how i can do it????i tried with google but nothing useful appeard..

posted 2008-06-02T23:27:20-07:00
Stretch

Market Level 2Community Level 4
295 posts

Math.floor(1.0387485749769);
Math.abs(1.0387485749769);

Look them up see which you need.

 

posted 2008-06-02T23:29:36-07:00 | edited 2008-06-02T23:30:07-07:00
xdragonx10

Market Level 6Community Level 6
287 posts

I think this is how you do it, not completely sure

Math.floor(your_number)

 

EDIT: >.<, I've been beat

posted 2008-06-02T23:31:21-07:00 | edited 2008-06-03T04:03:27-07:00
berto

Market Level 0Community Level 4
323 posts

i made a math operation

result=numer1 / number 2

 

//so if result is somenumber.03408560365467

it has to be some number

i tried all the functions but none worked... 

Edit: Thanks for the help anyway!!!!;)

posted 2008-06-02T23:47:09-07:00 | edited 2008-06-02T23:49:49-07:00
Stretch

Market Level 2Community Level 4
295 posts

try again / actually learn some actionscript.

posted 2008-06-02T23:49:11-07:00
berto

Market Level 0Community Level 4
323 posts

i can send the beta to you so you can see what i mean....i called the game Math training

 

posted 2008-06-02T23:51:02-07:00 | edited 2008-06-02T23:53:34-07:00
Ada111

Market Level 0Community Level 1
87 posts

Math.floor(your_number_variable);

Works like a charm :)

Assign your number in  avariable .

posted 2008-06-03T03:03:16-07:00
grado

Market Level 0Community Level 0
1255 posts

Math.round ? or is that deprecated or something?

Math.floor rounds down, Math.ceil rounds up, Math.round rounds to nearest integer. Math.abs makes the number positive

posted 2008-06-03T09:06:35-07:00
berto

Market Level 0Community Level 4
323 posts

thanks it worked now ;)

posted 2008-06-03T11:54:13-07:00
stroutsink

Market Level 0Community Level 1
57 posts

Or , if you're in AS2, and you're a code nut, you can use int(). Because int() is nanoseconds faster than Math.round.


for example:

var n:Number=3.4;

var m:Number=int(n);

trace(n+" = "+m); // traces 3.4 = 3

posted 2008-06-04T02:06:46-07:00
Jayc

Market Level 4Community Level 7
1004 posts

isn't int function deprecated since flash 5 in favor for Math.floor and ceil. will it still work properly on a flash8 AS2 project?

posted 2008-06-04T05:33:40-07:00
styxtwo

Market Level 4Community Level 7
705 posts

i think it does, there is also a randomize function that is deprecated but still works well so i think this one should work too.

posted 2008-06-04T11:26:11-07:00
stroutsink

Market Level 0Community Level 1
57 posts

Yeah, it still works despite having been deprecated, and being an intrinsic method as opposed to a static class method (like Math.floor) it's a lot faster. Try this:

var num:Number=5.344556;

var g:Number=0;

var m:Number=getTimer();

var i:Number=0;

for(i=0;i<100000;i++)g=int(num);

trace (getTimer()-m);

m=getTimer();

for(i=0;i<100000;i++)g=Math.floor(num);

trace(getTimer()-m);

 

That should give you an idea of how different they are.

 

posted 2008-06-04T16:57:25-07:00
Jayc

Market Level 4Community Level 7
1004 posts

I see, so deprecated intrinsic methods are faster the new static class methods.

I tested these and the results were:

int() = 255ms
Math.floor() = 369ms

random() = 277ms
Math.random() = 420ms

I'll be using int() and random() now instead of my ussual Math.floor( Math.random() * num ) since it returns the integer so no need to floor the result. Thanks for the heads up stroutsink.

Are there any other common methods/alternate code which can improve AS2 performance like these? I can't think of any other.

posted 2008-06-06T16:43:01-07:00 | edited 2008-06-06T16:45:00-07:00
stroutsink

Market Level 0Community Level 1
57 posts

Not really the same kind of method things, but as for optimizations, here are a few:

1- Declare local references to Math's static methods.

var sin:Function=Math.sin;

for(var i:Number=0;i &lt; 10000;i++)sin(10);

is faster than:

for(var i:Number=0;i &lt; 10000;i++)Math.sin(10);

 

2- When looping through Arrays, use the while loop trick to loop down, or the stored num trick to loop up

var i:Number=myArray.length;

while(i--){

myArray[i]++;

}

or:

var i:Number=0;

var l:Number=myArray.length;

for(; i &lt l;i++){

myArray[i]++;

}

Are both faster than:

for(var i:Number=0;i &lt myArray.length;i++)myArray[i]++;

 

3 - use Temporary Variables! (better on the eyes too)

var i:Number=items.length; // items is an array of Item objects

var itm:Item // note I declare it outside the loop!

while(i--){

   itm=items[i];

   // Do half a million things with itm here

}

 

is much faster (and prettier) than:

var i:Number=items.length; // items is an array of Item objects

while(i--){

   // Do half a million things with items[i] here

}

 

4 - The ? operator is faster than if/else.

var onground:Boolean=player.x>100?true:false;

is faster than:

if(player.x  &gt; 100)onground=true;

else onground=false;

 

And if that's not enough for you, go read this:

http://www.blog.lessrain.com/?p=118

posted 2008-06-06T21:02:43-07:00 | edited 2008-06-06T21:05:10-07:00
DougyTheFreshmaker

Market Level 0Community Level 2
62 posts

To extrapolate on statika's post--rounding, 'floor', and 'ceiling' are each significantly different.  I'm not sure how AS2 works, but forcing a conversion (casting) by using int() isn't deprecated in AS3, and simply truncates number, which is ALSO different than the others.

Consider the set of numbers {5.9, 5.1, -5.1, -5.9}

Rounding"finds the closest integer":  6, 5, -5,  -6

Flooring always "rounds down": 5, 5, -6, -6

Truncation gives you: 5, 5, -5, -5

And "ceiling" gives you: 6, 6, -5, -5

If AS2 doesn't support a Math.round type of function, you may need to code your own which follow rounding rules--replacing it with one of the others is clearly not a robust solution.


As well, all the optimizations mentioned in this thread are some serious micro-optimzations.  Clarity above speed until you've identified bottlenecks.  Additionally, many of the optimizations mentioned relate to common subexpression elimination, which can add speed while actually helping clarity, but it's important to understand exactly why those optimzations work and how an incorrect application of them could actually adversely affect performance.

posted 2008-06-06T22:52:20-07:00
Stretch

Market Level 2Community Level 4
295 posts

*leans out from behind Dougy and shakes fist*

Yeah you tell em!

posted 2008-06-06T23:07:06-07:00
scarybug

Market Level 6Community Level 6
881 posts

One thing I'm planning on trying. I have a game with forward movement based on rotation, which requires a lot of trig to find the new x and y values each step.

bug_mc._x +=Math.sin(Math.PI / 180 * ((-1) * bug_mc._rotation)) * speed;
bug_mc._y += Math.cos(Math.PI / 180 * ((-1) * bug_mc._rotation)) * speed;

I could take some of strout's suggestions...

var sin = Math.sin;

var cos= Math.cos;

var TRIG_CONST= (Math.PI/180)*(-1);

bug_mc._x += sin(TRIG_CONST * bug_mc._rotation)) * speed;
bug_mc._y += cos(TRIG_CONST * bug_mc._rotation)) * speed;

But, what if I just figure out what the x and y changes would be for the different values of the rotation? I could just estimate by only figuring out 16 different sections of the circle (groups of 22.5 degrees), or maybe divide a circle up in to 32 sections (11.25 degrees). I'd be storing 32 constants, and have a large switch/case block, but I'd avoid costly trig functions.

 

What do you think?

 

(Edit: Also, AS2 does support Math.round(), life would be pain without it!)

posted 2008-06-07T03:21:14-07:00 | edited 2008-06-07T03:28:01-07:00
DougyTheFreshmaker

Market Level 0Community Level 2
62 posts

I think you should profile your game and see if it ever even matters.  There's a pretty good chance it won't.

Optimization nearly always renders code much more rigid than it was in its original, well written state.  What if bound yourself to the 'caching' scheme you describe, then later decided to change your system to allow for ANY orientation?  Then you're totally screwed and everything breaks over a few nanoseconds here and there.

If you write your code in a reasonable way, and find this chunk to be a bottleneck later on, you can perform these sorts of optimizations *then*.  It'd be a sacrifice for performance's sake, but if you REALLY needed the cycles, then you'd just accept it.

In that case, you'd then have to run benchmarks/more profiling to determine if your optimizations would make any real difference (you've got things like cache coherency and stuff to worry about as well), as well as see how more 'elegant' ways of optimizing would work additionally.  For instance, the switch would be possibly the worst thing to do in terms of writing rigid code.  A table-based/data-driven approach could work better in those terms, but would probably negate any performance gains!  And then there's the fact that even constant look ups in AS take time, so you might consider running all of your code against a C (or m4 or whatever) preproceesor to let the preprocessor textually substitue the values in.  And in the end, the gains will likely be very minimal when taken in the context of your gain (they may be significant in a 1-million loop test, but not likely in any actual application).

posted 2008-06-07T04:16:41-07:00
Jayc

Market Level 4Community Level 7
1004 posts

I'm doing some more research now about optimizations especially on coding.

scarybug -> well it would reduce the possible ranges of the angle and the rotation speed would be a little problematic, i think. But it would be cool if you could test it and tell me if which is better.Wink

posted 2008-06-07T04:20:52-07:00