atof Replacement for ValD
Posted: Mon Oct 06, 2025 7:22 am
Following the hints in viewtopic.php?t=87644, I verified that using atof is actually much more faster than built-in ValD().
Speed test for 100000 string conversions:
ValD: 785ms
with atof: 494
Below a small code that could be used as replacement, just comment out the macro (PB 32bit, Windows).
Speed test for 100000 string conversions:
ValD: 785ms
with atof: 494
Below a small code that could be used as replacement, just comment out the macro (PB 32bit, Windows).
Code: Select all
ImportC ""
atof.d(*txt) As "_atof"
EndImport
; Macro ValD ;-> uncomment to use new procedure instead of built-in
; _ValD
; EndMacro
Procedure.d _ValD(str.s)
Protected *mem=Ascii(str)
dret.d=atof(*mem)
FreeMemory(*mem)
ProcedureReturn dret
EndProcedure
Define ret.d, i, u
Dim t$(10)
For i=0 To 10
t$(i)=Str(Random(32767,0))+"."+Random(32767,0)
Next
iStart=ElapsedMilliseconds()
For u=0 To 100000
For i=0 To 10
ret=ValD(t$(i))
Next i
Next u
Debug "Duration:"+Str(ElapsedMilliseconds()-iStart)
iStart=ElapsedMilliseconds()
For u=0 To 100000
For i=0 To 10
ret=_ValD(t$(i))
Next i
Next u
Debug "Duration:"+Str(ElapsedMilliseconds()-iStart)