atof Replacement for ValD

Just starting out? Need help? Post your questions and find answers here.
Oliver13
User
User
Posts: 92
Joined: Thu Sep 30, 2010 6:40 am

atof Replacement for ValD

Post by Oliver13 »

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).

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)
pjay
Enthusiast
Enthusiast
Posts: 277
Joined: Thu Mar 30, 2006 11:14 am

Re: atof Replacement for ValD

Post by pjay »

atof() doesn't work for me Oliver, I think it could be a 64 bit compatibility issue.

wtof() works though, and as this doesn't require the ascii conversion it's an order of magnitude faster. A downside for me was that it didn't handle conversion of PBs Binary (%) or Hexadecimal ($) prefixed strings.
User avatar
Piero
Addict
Addict
Posts: 1040
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: atof Replacement for ValD

Post by Piero »

Did I misunderstood something?

Here on Mac M1 _ValD (atof) is far slower…
Duration ValD : 72
Duration _ValD: 790

…and I'm supposed to be here to criticize Fred!
User avatar
NicTheQuick
Addict
Addict
Posts: 1527
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: atof Replacement for ValD

Post by NicTheQuick »

Three things:
1. You don't measure time in Debug mode.
2. An additional function call and string conversion adds a lot of time.
3. _atof does not exist on Linux it seems. But atof does. A version for UCS-2 strings like Purebasic uses them internally seems not to be available.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
NicTheQuick
Addict
Addict
Posts: 1527
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: atof Replacement for ValD

Post by NicTheQuick »

These are my results:

Code: Select all

 ValD: 103 ms
_ValD: 123 ms
Using this code and with the Debugger disabled, C-backend and code optimization enabled:

Code: Select all

EnableExplicit

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)
	Protected dret.d = atof(*mem)
	FreeMemory(*mem)
	ProcedureReturn dret
EndProcedure

OpenConsole()

Define ret.d, i, u, timeDiff

Dim t$(10)
For i = 0 To 10
	t$(i) = Str(Random(32767,0)) + "." + Random(32767,0)
Next

timeDiff = ElapsedMilliseconds()
For u = 0 To 100000
	For i = 0 To 10
		ret = ValD(t$(i))
	Next
Next

timeDiff = ElapsedMilliseconds() - timeDiff
PrintN(" ValD: " + timeDiff + " ms")



timeDiff = ElapsedMilliseconds()
For u = 0 To 100000
	For i = 0 To 10
		ret = _ValD(t$(i))
	Next
Next

timeDiff = ElapsedMilliseconds() - timeDiff
PrintN("_ValD: " + timeDiff + " ms")
Input()
CloseConsole()
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
Piero
Addict
Addict
Posts: 1040
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: atof Replacement for ValD

Post by Piero »

Just to try to criticize Fred: maybe calls to """ImportC""" are "not well implemented" for Mac ARM?

PS: only atof works here
Oliver13
User
User
Posts: 92
Joined: Thu Sep 30, 2010 6:40 am

Re: atof Replacement for ValD

Post by Oliver13 »

Tx for feedback, but differences seem actually to be caused by C/ASM mode and/or 32/64bit mode.

Compiling following code on x86, Windows, ASM mode, results here in a gain of speed with _ValdD by nearly 50% ... :D

PB 6.21: (with debugger)
ValD: 1193 ms
_ValD: 511 ms

PB 6.21: (no debugger)
ValD: 1144 ms
_ValD: 293 ms


PB 6.11: (with debugger)
ValD: 1197 ms
_ValD: 519 ms

PB 6.11: (no debugger)
ValD: 1211 ms
_ValD: 306 ms


PB 5.73: (with debugger)
ValD: 805 ms
_ValD: 497 ms

PB 5.73: (no debugger)
ValD: 767 ms
_ValD: 276 ms

PB 5.62: (with debugger)
ValD: 806 ms
_ValD: 491 ms

PB 5.62: (no debugger)
ValD: 760 ms
_ValD: 272 ms

Code: Select all

EnableExplicit

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)
	Protected dret.d = atof(*mem)
	FreeMemory(*mem)
	ProcedureReturn dret
EndProcedure

OpenConsole()

Define ret.d, i, u, timeDiff

Dim t$(10)
For i = 0 To 10
	t$(i) = Str(Random(32767,0)) + "." + Random(32767,0)
Next

timeDiff = ElapsedMilliseconds()
For u = 0 To 100000
	For i = 0 To 10
		ret = ValD(t$(i))
	Next
Next

timeDiff = ElapsedMilliseconds() - timeDiff
PrintN(" ValD: " + timeDiff + " ms")



timeDiff = ElapsedMilliseconds()
For u = 0 To 100000
	For i = 0 To 10
		ret = _ValD(t$(i))
	Next
Next

timeDiff = ElapsedMilliseconds() - timeDiff
PrintN("_ValD: " + timeDiff + " ms")
Input()
CloseConsole()

Btw,

Code: Select all

ImportC ""
	atof.d(*txt) As "atof"
EndImport
results in unresolved symbol error.
AZJIO
Addict
Addict
Posts: 2218
Joined: Sun May 14, 2017 1:48 am

Re: atof Replacement for ValD

Post by AZJIO »

1. If you use Random(), then be sure to use RandomSeed(), only then your results can be compared. Otherwise, they will depend on lucky numbers.
2. The result with the debugger is not interesting, as it is used for code quality and will not be used in reality.
Post Reply