Faster StrF()
Posted: Fri Aug 24, 2007 7:01 pm
With some simple tagliatelle it's possible to create a faster StrF() than Str() by exploiting the speed of Str():
Of course, speed test without debugger.
Code: Select all
Global Dim PrecisionLookup(9)
PrecisionLookup(0) = 0
PrecisionLookup(1) = 10
PrecisionLookup(2) = 100
PrecisionLookup(3) = 1000
PrecisionLookup(4) = 10000
PrecisionLookup(5) = 100000
PrecisionLookup(6) = 1000000
PrecisionLookup(7) = 10000000
PrecisionLookup(8) = 100000000
PrecisionLookup(9) = 1000000000
Procedure.s StrF2(A.f, Precision.l = 4)
Protected I.l
Protected Temp.s
Protected Minus.l
Precision = PrecisionLookup(Precision)
If A < 0
Minus = 1
A = -A
EndIf
I = A
If A < I
I - 1
EndIf
Temp = Str((A - I) * Precision + Precision)
If Minus
If Precision > 0
ProcedureReturn "-" + Str(I) + "." + Right(Temp, Len(Temp)-1)
Else
ProcedureReturn "-" + Str(I)
EndIf
Else
If Precision > 0
ProcedureReturn Str(I) + "." + Right(Temp, Len(Temp)-1)
Else
ProcedureReturn Str(I)
EndIf
EndIf
EndProcedure
#Tries = 500000
time = GetTickCount_()
For I = 0 To #Tries
StrF(1234.5678)
Next
MessageRequester("", Str(GetTickCount_()-time))
time = GetTickCount_()
For I = 0 To #Tries
StrF2(1234.5678)
Next
MessageRequester("", Str(GetTickCount_()-time))