:. NEWS .:. ABOUT .:. TRACK RECORD .:. BLOG .:. MUSIC .:. LINKS .:. GUESTBOOK .:
subscribe to feed via email

List of Entries ...

(1) [How to Delete Flash Cookies ... or How to find the Local Storage]
(2) [Safari 5, Flash And The Backspace Key ... Or How Apple Bans Flash from Safari]
(3) [ActionScript3 - Array Clear Performance Test (AS3)]
(4) [Flash-AS3 ByteArray Beats Array Class On Handling Large Data]
(5) [Overheating Acer AMD Turion 64x2 TL-58 Notebook problem]
(6) [Strange table behavior in Chromium / Google Chrom]
(7) [Getting Started With AsUnit 3.0 and FlashDevelop3 in AS3 (Unit Tests)]
(8) [Javascript Pixel Canvas]
(9) [{GERMAN} 3D-Visualisierung auf Mobiltelefonen mit J2ME]
(10) [{GERMAN} Voice Over IP (VoIP)]
(11) [{GERMAN} Parallele Schnittstelle]

- ActionScript3 - Array Clear Performance Test (AS3)

In your life as programmer you stumble upon one issue again and again: releasing/clearing an array. Normally, you would think deleting the contents is faster than creating a new array. With Flash this is not always the case.

Basically, there are four ways (I can think of) to clear an array:

  • Using length { array.length = 0 }
  • Using splice { array.splice(0,array.length) }
  • Using new { array = new Array() }
  • Using [ ] { array = [] }

Since it is not possible to simply identify the fastest way I made a small test application. It will create a specified number of arrays with a specified length and then clear the arrays using each of the four techniques. The arrays will be int-arrays. They are faster to create and to delete. Object arrays are just generally slower but display the same characteristics.

Try it out yourself. Change the parameters in the text fields below and hit "Start Tests". Please take into consideration that huge numbers take forever to complete.

Here is the code of the loops from the test app:

//////////////////////////////////////////////////////////////////
// using length
for (var i:int = 0; i < m_array.length; i++)
	m_array[i].length = 0;

//////////////////////////////////////////////////////////////////
// using splice
for (var i:int = 0; i < m_array.length; i++)
	m_array[i].splice(0, m_array[i].length);

//////////////////////////////////////////////////////////////////
// using new
for (var i:int = 0; i < m_array.length; i++)
	m_array[i] = new Array()

//////////////////////////////////////////////////////////////////
// using brackets
for (var i:int = 0; i < m_array.length; i++)
	m_array[i] = [];

When I did some systematic tests I found out that there is not the one ideal method. Depending on the test parameters one or the other may be faster. All tests are done on my AMD notebook using Firefox 3 and FlashPlayer 10. The code was build for FlashPlayer 9, though. I did each test five times and calculated the average, which are the values you will find below. (I´s interesting to note that the tests turn out slightly different on Intel machines.)

Well, let´s see. We will only look at two extremes for test parameters since my tests show a fairly linear scaling between the two.

Test 1: Few Huge Arrays

If you are running the tests with a relatively small number of arrays (10 to 100) and huge array lengths (10000 to 100.000) you will see that "new" and "[]" is the fastest. On my laptop they both take less than one millisecond to complete, while "splice" takes very long and "length" is only fairly fast.

__________________________________________
Parameters: number = 100, length = 100.000
length =  0       : 58 ms
splice(0, length) : 717 ms
new               : 0 ms
[]                : 0 ms

Test 2: Many Small Arrays

By switching the test parameters around the outcome is different. Here clearly setting the length is the fastest way while "splice" is the slowest again. "new" and "[]" are slower than setting length. Oddly, while "new" and "[]" do basically the same (creating a new array), using "[]" is almost twice as fast as "new". (This only seems to be the case on AMDs. On my Intel Dual Core both methods display equal performance.)

__________________________________________
Parameters: number = 100.000, length = 100
length =  0       : 74 ms
splice(0, length) : 911 ms
new               : 366 ms
[]                : 195 ms

Conclusions

length = 0
Setting the length is probably the most elegant solution. It is pretty fast in most cases and it makes a programmer feel good for not using new. It is not the best solution when it comes to performance but usually you don´t have so many arrays to clear. So, this would be my favorite for its prettiness.

splice (0, array.length)
Well, "splice" is terribly slow no matter what. I do not recommend using it at all for clearing.

new and []
A C or C++ programmer would probably wonder why this is actually the fastest solution for most real-world problems (small number of arrays with small length). Since I come from the C++ school of programming I personally do not like it. If you are really doing something absolutely performance critical involving a lot of arrays you may receive my blessing to use it, though. ;)

Postet on 2009/6/13


Kommentare / Comments:

(2010.07.07 13:53:43) vincent:
Thanks..


Kommentieren / Comment:

Name:

Email (optional):

 Wird nicht angezeigt. Will not be shown.

Kommentar/
Comment:

      

Note: Use plain text only. Javascript, html or css are not allowed.
Hinweis: Nur Klartext verwenden. Javascript, html und css sind nicht erlaubt.