Currently Adobe Flash is the leader in Internet multimedia. It’s been used to create a number of RIA (Rich Internet Application), web sites, games. Flash is used for the majority of banners and presentations. The number of possibilities grew when Action Script 3.0 came out and especially so with the release of Adobe Flash Player 10. The technology now provides for more powerful and feature-rich applications for the Internet. The number of games and all kinds of services ranging from upload managers to graphics editors almost as powerful as Photoshop is snowballing.
With Flash being so popular, there are a number of specialists out there and the market requires them to possess in-depth knowledge of the technology for better efficiency.
I’ve been working with flash for years and have learned a number of tricks to utilize Flash and hardware resources more efficiently. This document is not to be an optimization manual but rather a demonstration of some data types’ behavior on large computations, which is most often the case in Flash games. The goal was to explore such behavior and to present the results. What to do with these results is entirely up to you.
As an attempt to describe the most interesting aspects of optimization – below are the results of six tests. Five deal with data types and one is a test of class variable access:
Tests
Number vs. unit vs. int and * ………………………………………………………………………………………………… 1
String vs. * …………………………………………………………………………………………………………………………. 3
Array vs. Vector for a simple data type (Number)…………………………………………………………………….. 4
Array vs. Vector for complex data types (an class instance)……………………………………………………….. 5
Dot operator vs. Bracket operator…………………………………………………………………………………………… 7
Every test will be accompanied by two charts showing the results, the Y axis is time in milliseconds spent on 10^6 operations. There’s a download link at the end of the article to the archive with Flex/FlashDevelop projects. Should the reader like to verify any tests. All the tests were conducted on Flash Player 10.
Number vs. unit vs. int and *
in regards to «++», «+=» and «a=a+1». This test explores the similarly trivial increment operation in the context of different data types. (Test_1 in the archive).
The conclusion that can be drawn from the above – cast data types. There’s no performance advantage for either type. The use of different operators makes little difference as well.
String vs. *
for writing, reading and augmenting (+= operator is used to add one symbol) (Test_2)
The tests did not give any consistent results, perhaps some conditions were not met. Therefore it cannot be concluded that either of the types is faster. The only advantage of the String class is the presence of methods that provide for working with a line of characters.
Array vs. Vector for a simple data type (Number)
(Test_3_FD)
The read speed is about the same for both data types, but writing into Vector is faster, which could help to optimize some application. There’s a limitation though: Vector was only introduced in Flash Player 10, if the project requires backwards-compatibility with version 9, this optimization strategy cannot be used.
Array vs. Vector for complex data types (an class instance)
(Test_4_FD)
Here’s the code for the test:
{
private var _testvar:Number;
public var _testpublicvar:Number;
public function TestClass() {}
public function testFunc():void {
trace(“test”);
}
public function get testvar():Number { return _testvar; }
public function set testvar(value:Number):void
{
_testvar = value;
}
}
}
The huge difference between read and write performance comes from the delay needed to create an instance of TestClass. The test shows that Vector slightly inferior on write.
Dot operator vs. Bracket operator
(Test_6_FD)
The dot operator is slightly faster.
Public variable vs. setter/getter
(Test_5_FD)
Here’s the class:
package
{
/**
* …
* @author Shirobok Pavel aka ramshteks
*/
public class TestClass
{
public var testvar:uint;
public function TestClass() {}
public function setVar(value:uint):void {
testvar = value;
}
public function getVar():uint {
return testvar;
}
public function get testsetget():uint { return testvar; }
public function set testsetget(value:uint):void
{
testvar = value;
}
}
}
With either access type the public variable is faster, because no time is being spent on functions. On write the function and the setter are about equal. Getter is slightly quicker on read. As a result – we have a quick and dirty way to optimize the code at the expense of it’s quality.
To sum up, neither optimization approach is very effective on its own. Combining some of the above methods can dramaticaly improve performance on resource-demanding code.











