Faster access to multi-dimensional arrays...
Posted: Sun Jul 30, 2006 1:39 pm
Code updated for 5.20+
I've been reading up on game programming recently and have come across a nice little tip when using large multi-dimensional arrays.
When reading or writing values to such arrays and you need to optimise your code for maximum speed, you must make sure you write and read in 'Row Order' using Purebasic. This is because the arrays are stored in memory with the right most index's elements next to each other.
Check out this code:
You can see that when run, there is a big time difference writing using column ordered and row ordered access. I've usually used row access anyway in my programs but it's nice to know what speed is gained rather than using column access.
This tip only applies to Purebasic, other languages may differ.
I've been reading up on game programming recently and have come across a nice little tip when using large multi-dimensional arrays.
When reading or writing values to such arrays and you need to optimise your code for maximum speed, you must make sure you write and read in 'Row Order' using Purebasic. This is because the arrays are stored in memory with the right most index's elements next to each other.
Check out this code:
Code: Select all
#ELEMENTS = 250
Global Dim TestData.f(#ELEMENTS, #ELEMENTS, #ELEMENTS)
StartTime = ElapsedMilliseconds()
;Column Ordered
For z = 0 To #ELEMENTS
For y = 0 To #ELEMENTS
For x = 0 To #ELEMENTS
TestData(x, y, z) = 0.0
Next x
Next y
Next z
ColumnTime = ElapsedMilliseconds() - StartTime
StartTime = ElapsedMilliseconds()
;Row Ordered
For x = 0 To #ELEMENTS
For y = 0 To #ELEMENTS
For z = 0 To #ELEMENTS
TestData(x, y, z) = 0.0
Next z
Next y
Next x
RowTime = ElapsedMilliseconds() - StartTime
MessageRequester("Timings", "Column Ordered: " + Str(ColumnTime) + " ms" + #LF$ + "Row Ordered: " + Str(RowTime) + " ms" + #LF$ + "Delta: " + Str(ColumnTime - RowTime))
This tip only applies to Purebasic, other languages may differ.
