Int vs. Round
From ActionScript Performance Wiki
Contents |
Purpose
Test if it is faster to convert a number to an integer or to perform Math.round().
Code
package { import flash.display.Sprite; /** * @author Nicolas Schudel */ public class Test extends Sprite { public var a:Number; public var b:Number = 1.056684651654654; public var c:int; public function Test() { trace(new PerformanceComparison(1000000, test1, test2, test3, test4).start()); } private function test1():void { a = Math.round(b); } private function test2():void { a = int(b); } private function test3():void { a = b as int; } private function test4():void { c = b; } } }
Results
Compiler version: 3.3.0.485, Player version: MAC 10.0.2.54, Operating System: Mac OS 10.5.8
- Test 1
- 628 ms, 37% slower
- Test 2
- 460 ms, best
- Test 3
- 597 ms, 30% slower
- Test 4
- 469 ms, 02% slower
Compiler version: 3.3.04852, Player version: WIN 10.0.45.2, Operating System: Windows XP
- Test 1
- 455 ms, 46% slower
- Test 2
- 313 ms, 01% slower
- Test 3
- 498 ms, 60% slower
- Test 4
- 312 ms, best
Conclusion
Type casting a number to an integer is the fastest way to round a number. Surprisingly int(n) is faster than n as int.

