271

**GLBasic - en / Re: Array speeds**

« **on:**2012-Feb-28 »

Interesting topic, so just for comparison same code in FreeBasic:

And my results on P E2180 (repeated several times):

So basically in GLB is better to use column order

Code: GLBasic [Select]

dim as double TimeStart, TimeEnd, TimeTaken, DummyVar

dim as integer n,n2, i, j, k

n=250

n2=10000

reDIM as integer TestData3d(n,n,n)

cls

sleep 1000

PRINT "Writing Data to the 3d array"

'' Fill 3d array in Column order

TimeStart = timer()

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 = timer()

TimeTaken = TimeEnd - TimeStart

PRINT "Column order took = "+str(TimeTaken)

'' Fill 3d array in Row order

TimeStart = timer()

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 = timer()

TimeTaken = TimeEnd - TimeStart

PRINT "Row order took = "+str(TimeTaken)

sleep 1000

PRINT "Reading Data from the 3d array"

'' Read 3d array in Column order

TimeStart = timer()

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 = timer()

TimeTaken = TimeEnd - TimeStart

PRINT "Column order took = "+str(TimeTaken)

'' Read 3d array in Row order

TimeStart = timer()

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 = timer()

TimeTaken = TimeEnd - TimeStart

PRINT "Row order took = "+str(TimeTaken)

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

redim TestData3d(0,0,0)

reDIM as integer TestData2d(n2,n2)

sleep 1000

PRINT "Writing Data to the 2d array"

'' Fill 2d array in Column order

TimeStart = timer()

FOR j=0 TO n2-1

FOR i=0 TO n2-1

TestData2d(i,j)=0

NEXT

NEXT

TimeEnd = timer()

TimeTaken = TimeEnd - TimeStart

PRINT "Column order took = "+str(TimeTaken)

'' Fill 2d array IN Row order

TimeStart = timer()

FOR i=0 TO n2-1

FOR j=0 TO n2-1

TestData2d(i,j)=0

NEXT

NEXT

TimeEnd = timer()

TimeTaken = TimeEnd - TimeStart

PRINT "Row order took = "+str(TimeTaken)

sleep 1000

PRINT "Reading Data from the 2d array"

'' Read 2d array in Column order

TimeStart = timer()

FOR j=0 TO n2-1

FOR i=0 TO n2-1

DummyVar=TestData2d(i,j)

NEXT

NEXT

TimeEnd = timer()

TimeTaken = TimeEnd - TimeStart

PRINT "Column order took = "+str(TimeTaken)

'' Read 2d array in Row order

TimeStart = timer()

FOR i=0 TO n2-1

FOR j=0 TO n2-1

DummyVar=TestData2d(i,j)

NEXT

NEXT

TimeEnd = timer()

TimeTaken = TimeEnd - TimeStart

PRINT "Row order took = "+str(TimeTaken)

sleep

dim as integer n,n2, i, j, k

n=250

n2=10000

reDIM as integer TestData3d(n,n,n)

cls

sleep 1000

PRINT "Writing Data to the 3d array"

'' Fill 3d array in Column order

TimeStart = timer()

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 = timer()

TimeTaken = TimeEnd - TimeStart

PRINT "Column order took = "+str(TimeTaken)

'' Fill 3d array in Row order

TimeStart = timer()

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 = timer()

TimeTaken = TimeEnd - TimeStart

PRINT "Row order took = "+str(TimeTaken)

sleep 1000

PRINT "Reading Data from the 3d array"

'' Read 3d array in Column order

TimeStart = timer()

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 = timer()

TimeTaken = TimeEnd - TimeStart

PRINT "Column order took = "+str(TimeTaken)

'' Read 3d array in Row order

TimeStart = timer()

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 = timer()

TimeTaken = TimeEnd - TimeStart

PRINT "Row order took = "+str(TimeTaken)

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

redim TestData3d(0,0,0)

reDIM as integer TestData2d(n2,n2)

sleep 1000

PRINT "Writing Data to the 2d array"

'' Fill 2d array in Column order

TimeStart = timer()

FOR j=0 TO n2-1

FOR i=0 TO n2-1

TestData2d(i,j)=0

NEXT

NEXT

TimeEnd = timer()

TimeTaken = TimeEnd - TimeStart

PRINT "Column order took = "+str(TimeTaken)

'' Fill 2d array IN Row order

TimeStart = timer()

FOR i=0 TO n2-1

FOR j=0 TO n2-1

TestData2d(i,j)=0

NEXT

NEXT

TimeEnd = timer()

TimeTaken = TimeEnd - TimeStart

PRINT "Row order took = "+str(TimeTaken)

sleep 1000

PRINT "Reading Data from the 2d array"

'' Read 2d array in Column order

TimeStart = timer()

FOR j=0 TO n2-1

FOR i=0 TO n2-1

DummyVar=TestData2d(i,j)

NEXT

NEXT

TimeEnd = timer()

TimeTaken = TimeEnd - TimeStart

PRINT "Column order took = "+str(TimeTaken)

'' Read 2d array in Row order

TimeStart = timer()

FOR i=0 TO n2-1

FOR j=0 TO n2-1

DummyVar=TestData2d(i,j)

NEXT

NEXT

TimeEnd = timer()

TimeTaken = TimeEnd - TimeStart

PRINT "Row order took = "+str(TimeTaken)

sleep

And my results on P E2180 (repeated several times):

Code: GLBasic [Select]

GLB FB

writing to 3d array

column order: 0.170 sec - 0.703 sec

row order: 0.699 sec - 0.089 sec

reading from 3d array

colum order: 0.137 sec - 0.65 sec

row order: 0.120 sec - 0.086 sec

writing to 2d array

column order: 0.986 sec - 8.092 sec

row order: 6.791 sec - 0.808 sec

reading from 2d array

column order: 0.656 sec - 5.20 sec

row order: 0.684 sec - 0.455 sec

writing to 3d array

column order: 0.170 sec - 0.703 sec

row order: 0.699 sec - 0.089 sec

reading from 3d array

colum order: 0.137 sec - 0.65 sec

row order: 0.120 sec - 0.086 sec

writing to 2d array

column order: 0.986 sec - 8.092 sec

row order: 6.791 sec - 0.808 sec

reading from 2d array

column order: 0.656 sec - 5.20 sec

row order: 0.684 sec - 0.455 sec

So basically in GLB is better to use column order