|
|
|
List of Entries ... - 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:
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. (It?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 ArraysIf 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 ArraysBy 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 Conclusionslength = 0 splice (0, array.length) new and [] Postet on 2009/6/13 Kommentieren / Add comment... (0) |