The task is simple and typical. There is a large two-dimensional array. And we want to work with him most effectively. In my case, I was interested in an array of numbers (bytes).
Well ... The simplest thing that comes to mind is to use a standard two-dimensional array of type and access elements in the manner of a [x] [y]. But how effective is it? Perhaps it will be faster to operate with a one-dimensional array and access elements as a [x + y * size_x]?
To dot the i's, I wrote a test that checks the speed of reading and writing various variants of arrays.
')
The following options came to mind:
- 2-dimensional untyped array (Array)
- 1-dimensional untyped array (Array)
- 2-dimensional typed array (Vector. <Vector. <Int >>)
- 1-dimensional typed array (Vector. <Int>)
- Using BitmapData as a repository and setPixel / getPixel for access
- 1-dimensional byte array (ByteArray)
- Well, finally, perverted. Appeal to ByteArray, accelerated by means of fast memory access technology Alchemy
So, I wrote a small test to compare the speed of code execution. In the test, an imitation of reading and writing all elements of an array of 1500x1500 points was made. Each operation was performed 5 times and the average time was taken.
Also, naturally, dynamic arrays were pre-created and filled with elements (i.e., new elements were not added during the test measurements).
So, what happened:

Diagram:

The conclusions are obvious.
Source code posted here:
wonderfl.net/c/d58d