Hello again,
Some speed test here with all proposed solutions.
Code: Select all
Procedure.f FastInverseSquareRootPeek(Number.f)
x2.f = Number * 0.5
y.f = Number
i.l = PeekL(@y)
i = $5f3759df - (i >> 1)
y = PeekF(@i)
y = y * (1.5 - (x2 * y * y))
ProcedureReturn y
EndProcedure
Structure _H_
StructureUnion
l.l
f.f
EndStructureUnion
EndStructure
Procedure.f FastInverseSquareRootStruct(Number.f)
Protected *i._H_, x2.f, y.f
x2 = Number * 0.5
y = Number
*i = @y
*i\l = $5f3759df - (*i\l >> 1)
y = *i\f
y = y * (1.5 - (x2 * y * y))
ProcedureReturn y
EndProcedure
Procedure.f FastInverseSquareRootCopyMem(Number.f)
x2.f = Number * 0.5
y.f = Number
CopyMemory(@y, @i.l, 4)
i = $5f3759df - (i >> 1)
CopyMemory(@i, @y, 4)
y = y * (1.5 - (x2 * y * y))
;y = y * (1.5 - (x2 * y * y))
ProcedureReturn y
EndProcedure
TempsDepart0 = ElapsedMilliseconds()
For TestID = 0 To 10000000
A.f = FastInverseSquareRootStruct(5.0)
Next
TempsEcoule0 = ElapsedMilliseconds() - TempsDepart0
TempsDepart1 = ElapsedMilliseconds()
For TestID = 0 To 10000000
A.f = FastInverseSquareRootCopyMem(5.0)
Next
TempsEcoule1 = ElapsedMilliseconds() - TempsDepart1
TempsDepart2 = ElapsedMilliseconds()
For TestID = 0 To 10000000
A.f = FastInverseSquareRootPeek(5.0)
Next
TempsEcoule2 = ElapsedMilliseconds() - TempsDepart2
TempsDepart3 = ElapsedMilliseconds()
For TestID = 0 To 10000000
B.f = 1.0 / Sqr(5.0)
Next
TempsEcoule3 = ElapsedMilliseconds() - TempsDepart3
MessageRequester("Temps FastInverseSquareRoot(5.0) vs 1.0 / Sqr(5.0)", "With Struct Union -> " + Str(TempsEcoule0) + #LF$ +
"With Copy Memory -> " + Str(TempsEcoule1) + #LF$ +
"With PeekL/F -> " + Str(TempsEcoule2) + #LF$ +
"Reference -> " + Str(TempsEcoule3) + #LF$)
There is the result I get :
With Struct Union -> 193 ms
With Copy Memory -> 189 ms
With PeekL/F -> 175 ms
Reference -> 32 ms
1.0 / Sqr(5.0) is faster, no performance gain at all with FastInverseSquareRoot() so ...
Best regards
StarBootics