Math.floor Vs bit shifting Vs int

I just ran some tests on round numbers.

In preparation for getting max performance from AS3, I want to test as many areas as possible.

All I did was generate a rounded random number a million times per frame, and record the time step in milliseconds.

These are the results -

Math.floor = 303 ms

bitshifting = 220 ms

int = 205 ms

Now the strangest part is having a non rounded random number generated a million times per frame netted an average time of 195 milliseconds, faster than using int as a random number. It must simply be that because Math.random isn’t returning an integer, some form of conversion of taking place for your variable to be an integer.

Incidentally, using a Number instead of integer for the looping variable decreased performance minimally, just a few milliseconds.

So simply having your variable as an integer, and using no rounding at all produces the fastest results if you want a round number.

So where practical, just do that.

Remember though, there are many other areas in Flash to dramatically increase performance. Being that we’re talking about a million iterations per frame, the difference in performance is not exactly dramatic here. One is not even twice as fast as another, over a million iterations.

But still, every bit of juice counts in Flash :wink: