facebook logo

Click here to get a complete list of blogentries.


Flash-AS3 ByteArray Beats Array Class On Handling Large Data

For a map generator, I recently had to figure out a way to fit about 10 million values (representing the map data) into memory and work with them efficiently. Flash isn't exactly cut out for this task but I had very good reasons to go through the pain and implement it in AS3 anyway.

There are several approaches to solve this problem. To investigate further into this I created a small testing suite. This is how it works:

The test suite contains one test class (cDataHandlingTest) that writes to and reads from an abstract data class (iData) . Each data class offers an unified interface but is implemented in different ways. The test class repeats each test several times, records initialization times and accessing times and then calculates the average for each data class. If you want to have a closer look on the code, you can download my FlashDevelop-project here.

Three-Dimensional Array (cDataArray3D)

This data class uses a three-dimensional array to store the data. Initialization takes a very long time because you need to cycle through several nested loops allocating the data. Access times are ok.

Linear Array (cDataArray1D)

Here I used a linear array for the data. Initialization for a linear area is lightning fast. The draw-back is the access, though. Calculating the offset of the data in the array is pretty complex:


var index:int =  (((_y * m_width ) + _x) * m_entries) + _entryType;

This makes working with a linear array really slow if you cannot predict in what order you will access the data. If you will usually access the data in the exact order it is stored in the array, you would actually be really fast. Unfortunately, this is usually not the case. The linear array is about 10 times slower than the three-dimensional one on random access.

ByteArray Int-Based Access (cDataArrayByte)

In this example I used a ByteArray. The position is calculated in the exact same way as in the linear array example. Data is extracted by using a readInt() command on the stream, data is written by using the writeInt() command. I expected this approach to be really slow. Well it isn't! On my computer it already outperforms the three-dimensional array in all areas. I tried this test on many different computers and they did not show consistent behavior. On some the byte array was faster and on some it was not. It seems to me that computers with AMD processors were faster with ByteArray while computers powered by Intel were faster with the three-dimensional array.

ByteArray Byte-Based Access (cDataArrayByteOpt)

If you only need values between 0 and 255 you can use the array operator [ ] of the ByteArray class. This is the fastest way to access the data. Initialization times are really good and access is on my computer about 25% faster than the three-dimensional array.

Test Application

This is the test. Click into the window to start. Be careful, it needs a lot of resources. If you have significantly less then 2GHz per core or only one core I recommend not to try it because it will lock up your computer for too long to be convenient.

Posted on 2009/3/29
blog comments powered by Disqus
homecoded facebook logo
Imprint:
This is a picture of my address. I am not going to put that in plain writing!
Small print:
disclaimer
Important People:
Jörg Winterstein
Jazzoid
Dirk Schreiter
Thomas Schreiter
Chris Noeth
Rafael van Daele-Hunt
Lars Kranebitter
Ronny Kober
Important Links:
sucodo (plagiarism detection)
Gameforge Inside
Hacker News
Golem
Heise
SLAY RADIO