While reading an old programming book (2003 so not uber old) I got onto a section about memory access, cache access etc & found something I thought was interesting so decided to test it in GLB. The book is about game coding & the code is in C++ but it's the concepts I was reading it for. Most will prob not find it interesting or any use for it but I found it of interest

Basically (no pun intended) it was saying about how accessing arrays in the wrong order can have an effect on speed & cache hits. His results had more dramatic differences then mine but that was probably down the the cpu's of the time & their respective cache sizes, however I did witness noticeable differences with my translated version of the code.

`LOCAL n%,n2%, i%, j%, k%, TimeStart, TimeEnd, TimeTaken, DummyVar`

n=250

n2=10000

DIM TestData3d[n][n][n]

PRINT "Writing Data to the 3d array",0,0

// Fill 3d array in Column order

TimeStart = GETTIMERALL()

FOR k=0 TO n-1

FOR j=0 TO n-1

FOR i=0 TO n-1

TestData3d[i][j][k]=0

NEXT

NEXT

NEXT

TimeEnd = GETTIMERALL()

TimeTaken = TimeEnd - TimeStart

PRINT "Column order took = "+TimeTaken,0,10

// Fill 3d array in Row order

TimeStart = GETTIMERALL()

FOR i=0 TO n-1

FOR j=0 TO n-1

FOR k=0 TO n-1

TestData3d[i][j][k]=0

NEXT

NEXT

NEXT

TimeEnd = GETTIMERALL()

TimeTaken = TimeEnd - TimeStart

PRINT "Row order took = "+TimeTaken,0,20

SHOWSCREEN

MOUSEWAIT

PRINT "Reading Data from the 3d array",0,0

// Read 3d array in Column order

TimeStart = GETTIMERALL()

FOR k=0 TO n-1

FOR j=0 TO n-1

FOR i=0 TO n-1

DummyVar=TestData3d[i][j][k]

NEXT

NEXT

NEXT

TimeEnd = GETTIMERALL()

TimeTaken = TimeEnd - TimeStart

PRINT "Column order took = "+TimeTaken,0,10

// Read 3d array in Row order

TimeStart = GETTIMERALL()

FOR i=0 TO n-1

FOR j=0 TO n-1

FOR k=0 TO n-1

DummyVar=TestData3d[i][j][k]

NEXT

NEXT

NEXT

TimeEnd = GETTIMERALL()

TimeTaken = TimeEnd - TimeStart

PRINT "Row order took = "+TimeTaken,0,20

SHOWSCREEN

MOUSEWAIT

// Clear 3d array from mem & create 2d one

DIM TestData3d[0][0][0]

DIM TestData2d[n2][n2]

PRINT "Writing Data to the 2d array",0,0

// Fill 2d array in Column order

TimeStart = GETTIMERALL()

FOR j=0 TO n2-1

FOR i=0 TO n2-1

TestData2d[i][j]=0

NEXT

NEXT

TimeEnd = GETTIMERALL()

TimeTaken = TimeEnd - TimeStart

PRINT "Column order took = "+TimeTaken,0,10

// Fill 2d array IN Row order

TimeStart = GETTIMERALL()

FOR i=0 TO n2-1

FOR j=0 TO n2-1

TestData2d[i][j]=0

NEXT

NEXT

TimeEnd = GETTIMERALL()

TimeTaken = TimeEnd - TimeStart

PRINT "Row order took = "+TimeTaken,0,20

SHOWSCREEN

MOUSEWAIT

PRINT "Reading Data from the 2d array",0,0

// Read 2d array in Column order

TimeStart = GETTIMERALL()

FOR j=0 TO n2-1

FOR i=0 TO n2-1

DummyVar=TestData2d[i][j]

NEXT

NEXT

TimeEnd = GETTIMERALL()

TimeTaken = TimeEnd - TimeStart

PRINT "Column order took = "+TimeTaken,0,10

// Read 2d array in Row order

TimeStart = GETTIMERALL()

FOR i=0 TO n2-1

FOR j=0 TO n2-1

DummyVar=TestData2d[i][j]

NEXT

NEXT

TimeEnd = GETTIMERALL()

TimeTaken = TimeEnd - TimeStart

PRINT "Row order took = "+TimeTaken,0,20

SHOWSCREEN

MOUSEWAIT

BTW originally they where separate tests I wrote but then lumped them into one prog & the results where still the same, also trying ints rather than floats produced the same results.

On my system filling the 3d array was on average twice as fast in column order then row order, while reading there was little in it but row order was slightly quicker. The difference does scale with the the size of the array for example the 2d fill with 10000x10000 elements was 3.5 times quicker in column order whereas with 5000x5000 elements around 2 times quicker.

How different the results would be on other platforms like iPhone etc I do not know as not having any of the other platforms I'm unable to test.

If you are filling any large arrays in your project during runtime (except at the initial start) it might be an idea to run this test on the device to see what works quicker if at all. Who knows you may gain a few millisecs that could be used elsewhere or give a boost to updates

Lee