Test ActionScript 3.0 `Math.ceil()` function performance

pihao
2014-09-10T14:58:52Z

as3_math_ceil_function_performance.as

| package
| {
| import flash.display.Sprite;
| import flash.utils.getTimer;
|
| /**
| * @author pihao
| * Environment:
| * Macbook Pro with OS X 10.9.4
| * Flash Player 15.0.0.152
| * AIR SDK 14.0.0
| */
| public class Test extends Sprite
| {
| public function Test()
| {
| var count:uint = 1e6;
|
| var t0:uint = getTimer();
| for (var i:int = 1; i < count; i++) Math.ceil(1/i);
| var t1:uint = getTimer();
| for (i = 1; i < count; i++) mceil(1/i);
| var t2:uint = getTimer();
| for (i = 1; i < count; i++) ceil(1/i);
| var t3:uint = getTimer();
|
| trace(t1 - t0, "ms"); // Math.ceil: about 70ms
| trace(t2 - t1, "ms"); // mceil: about 300ms
| trace(t3 - t2, "ms"); // ceil: about 300ms
| }
|
| private function mceil(n:Number):int
| {
| return Math.ceil(n);
| }
|
| private function ceil(n:Number):int
| {
| var m:int = n;
| return (n > 0 && n != m) ? m + 1 : m;
| }
|
| }
| }