Matrix Structure Speed test
Posted: Sat Mar 20, 2021 11:48 pm
Hello everyone,
Apparently the use of static arrays inside structures are pretty darn slow in comparison of just straight fields.
When I run the code above this is what I get
Matrix4f : 10 milliseconds
Matrix44f : 8 milliseconds
Matrix44 : 2 milliseconds
Matrix4f : 13 milliseconds
Matrix44f : 8 milliseconds
Matrix44 : 2 milliseconds
Matrix4f : 13 milliseconds
Matrix44f : 8 milliseconds
Matrix44 : 2 milliseconds
Matrix4f : 12 milliseconds
Matrix44f : 8 milliseconds
Matrix44 : 2 milliseconds
Matrix4f : 12 milliseconds
Matrix44f : 9 milliseconds
Matrix44 : 2 milliseconds
The question is what is making the use of static arrays so slow ? The memory offset calculation ? The nested for loops ? Both, memory offset calculation and nested for loops ?
Thanks beforehand.
StarBootics
Apparently the use of static arrays inside structures are pretty darn slow in comparison of just straight fields.
Code: Select all
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Project name : Matrix Structure Speed test
; File Name : Matrix Structure Speed test.pb
; File version: 1.0.0
; Programming : OK
; Programmed by : StarBootics
; Date : 20-03-2021
; Last Update : 20-03-2021
; PureBasic code : V5.73LTS
; Platform : Windows, Linux, MacOS X
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Structure Line
Element1D.f[4]
EndStructure
Structure Matrix4f
Element2D.Line[4]
EndStructure
Structure Matrix44f
e.f[16]
EndStructure
Structure Matrix44
e11.f
e21.f
e31.f
e41.f
e12.f
e22.f
e32.f
e42.f
e13.f
e23.f
e33.f
e43.f
e14.f
e24.f
e34.f
e44.f
EndStructure
Macro AccessMatrix44f(MatrixA, Line, Column)
MatrixA\e[Column * 4 + Line]
EndMacro
Macro AccessMatrix4f(MatrixA, Line, Column)
MatrixA\Element2D[Column]\Element1D[Line]
EndMacro
Procedure Identity4f(*MatrixA.Matrix4f)
For i = 0 To 3
For j = 0 To 3
If i = j
AccessMatrix4f(*MatrixA, i, j) = 1.0
Else
AccessMatrix4f(*MatrixA, i, j) = 0.0
EndIf
Next
Next
EndProcedure
Procedure Identity44f(*MatrixA.Matrix44f)
For i = 0 To 3
For j = 0 To 3
If i = j
AccessMatrix44f(*MatrixA, i, j) = 1.0
Else
AccessMatrix44f(*MatrixA, i, j) = 0.0
EndIf
Next
Next
EndProcedure
Procedure Identity44(*Identity.Matrix44)
*Identity\e11 = 1.0
*Identity\e21 = 0.0
*Identity\e31 = 0.0
*Identity\e41 = 0.0
*Identity\e12 = 0.0
*Identity\e22 = 1.0
*Identity\e32 = 0.0
*Identity\e42 = 0.0
*Identity\e13 = 0.0
*Identity\e23 = 0.0
*Identity\e33 = 1.0
*Identity\e43 = 0.0
*Identity\e14 = 0.0
*Identity\e24 = 0.0
*Identity\e34 = 0.0
*Identity\e44 = 1.0
EndProcedure
For TestID = 0 To 4
TempsDepart0 = ElapsedMilliseconds()
For Index = 0 To 100000
Identity4f(MatrixA.Matrix4f)
Next
TempsEcoule0 = ElapsedMilliseconds()-TempsDepart0
TempsDepart1 = ElapsedMilliseconds()
For Index = 0 To 100000
Identity44f(MatrixB.Matrix44f)
Next
TempsEcoule1 = ElapsedMilliseconds()-TempsDepart1
TempsDepart2 = ElapsedMilliseconds()
For Index = 0 To 100000
Identity44(MatrixC.Matrix44)
Next
TempsEcoule2 = ElapsedMilliseconds()-TempsDepart2
MessageRequester("Elapsed Time", "Matrix4f : " + Str(TempsEcoule0) + " milliseconds" + #LF$ + "Matrix44f : " + Str(TempsEcoule1) +" milliseconds"+ #LF$ + "Matrix44 : " + Str(TempsEcoule2) +" milliseconds")
Next
; <<<<<<<<<<<<<<<<<<<<<<<
; <<<<< END OF FILE <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<
Matrix4f : 10 milliseconds
Matrix44f : 8 milliseconds
Matrix44 : 2 milliseconds
Matrix4f : 13 milliseconds
Matrix44f : 8 milliseconds
Matrix44 : 2 milliseconds
Matrix4f : 13 milliseconds
Matrix44f : 8 milliseconds
Matrix44 : 2 milliseconds
Matrix4f : 12 milliseconds
Matrix44f : 8 milliseconds
Matrix44 : 2 milliseconds
Matrix4f : 12 milliseconds
Matrix44f : 9 milliseconds
Matrix44 : 2 milliseconds
The question is what is making the use of static arrays so slow ? The memory offset calculation ? The nested for loops ? Both, memory offset calculation and nested for loops ?
Thanks beforehand.
StarBootics