AS3. Optimization: initializing Object and Array

Testing environment

You will find many tables that covers execution time. All this are done on machine:

Comp1
Intel(R) Core(TM) Duo CPU E8400 @ 3000Hz 3.00 GHz, 3 .00 GB of RAM with Windows XP Professional SP3, Flash player version 9.0.115.0, NOT DEBUG PLAYER

All the tests were not done using the debug player and they were not running in debug mode which is very important in specific cases. Each test case was running ten times. The result shown is always the average execution time of the ten runs. This way two tests do not conflict with the garbage collection and each test is just responsible for its own memory usage. Before each run there is a ten seconds delay in order to not conflict with the Flash player starting procedure.

From result we see that initializing objects and arrays with new constructor is slower then directly with {} and []. So it may be useful to keep in mind that when you creating objects or arrays use:
var array:Array = [];
var obj:Object = {};

instead of
var array:Array = new Array();
var obj:Object = new Object();

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package {
   
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.utils.getTimer;
    import flash.utils.setTimeout;
    import flash.system.Capabilities;
   
    [SWF(frameRate = '255')]
   
    public class TestCase extends Sprite {
        private static const n: int = 100000;
       
       
        public function TestCase() {
            setTimeout(runTest, 10000);
        }
       
        private function runTest(): void {
            var a: Number = 0;
            for ( var i: int = 0; i < 10; ++i ) a += test03();
            a /= 10;
            showResults(a);
        }
       
        private function showResults(result_:Number):void {
            var result_txt:TextField = new TextField();
            result_txt.x = result_txt.y = 10;
            result_txt.width = 300;
            result_txt.text = "Iterations: [10^" + (String(n).length - 1) + "], int[ms]: " + result_ +"\nisDebugger: " + Capabilities.isDebugger + "\nversion: " + Capabilities.version;
            addChild(result_txt);
        }
       
       
        // 1. Iterations: [10^5], int[ms]: 28.6
        // 2. Iterations: [10^5], int[ms]: 28.7
        private function test00(): int {
            var startTime:int = getTimer();
           
            //test start
            for (var i:int = 0; i < n; ++i ) {
                var obj:Object = new Object();
            }
           
            //test end
            return (getTimer() - startTime);
        }
       
        // 1. Iterations: [10^5], int[ms]: 22.1
        // 2. Iterations: [10^5], int[ms]: 22.1
        // 3. Iterations: [10^5], int[ms]: 22.6
        private function test01(): int {
            var startTime:int = getTimer();
           
            //test start
            for (var i:int = 0; i < n; ++i ) {
                var obj:Object = { };
            }
           
            //test end
            return (getTimer() - startTime);
        }
       
        // 1. Iterations: [10^5], int[ms]: 58.8
        // 2. Iterations: [10^5], int[ms]: 58.8
        private function test02(): int {
            var startTime:int = getTimer();
           
            //test start
            for (var i:int = 0; i < n; ++i ) {
                var array:Array = new Array();
            }
           
            //test end
            return (getTimer() - startTime);
        }
       
        // 1. Iterations: [10^5], int[ms]: 23.7
        // 2. Iterations: [10^5], int[ms]: 24
        // 3. Iterations: [10^5], int[ms]: 23.6
        private function test03(): int {
            var startTime:int = getTimer();
           
            //test start
            for (var i:int = 0; i < n; ++i ) {
                var obj:Array = [];
            }
           
            //test end
            return (getTimer() - startTime);
        }
    }
}
Creative Commons License
This work, unless otherwise expressly stated, is licensed under a Creative Commons Attribution 2.5 Bulgaria License.

Comments are closed.