ValF() speed

Share your advanced PureBasic knowledge/code with the community.
User avatar
Otrebor
Enthusiast
Enthusiast
Posts: 200
Joined: Mon Mar 17, 2014 1:42 pm
Location: São Paulo, Brasil
Contact:

ValF() speed

Post by Otrebor »

Probably nothing new here :)
But...converting some float stored in strings, i found that ValF(), in this case, is not the best option (at least in my computer).

Code: Select all

a$="-0.15"
b$="11.89"
c$="-0.01"


xtime=ElapsedMilliseconds()
For  f = 1 To 100000
  a = ValF(a$)*100
  b = ValF(b$)*100
  c = ValF(c$)*100
Next
MessageRequester("",Str(ElapsedMilliseconds()-xtime))

xtime=ElapsedMilliseconds()
For  f = 1 To 100000
  a = Val(RemoveString(a$,"."))
  b = Val(RemoveString(b$,"."))
  c = Val(RemoveString(c$,"."))
Next
MessageRequester("",Str(ElapsedMilliseconds()-xtime))
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: ValF() speed

Post by davido »

@Otrebor,
Interesting!
I don't know on what machine you tested the code, however, on my MacBook, Val() was over 4x faster.
I noted that replacing ValF with ValD, Val() was only 3x faster!

Perhaps someone can explain why?
DE AA EB
User avatar
Otrebor
Enthusiast
Enthusiast
Posts: 200
Joined: Mon Mar 17, 2014 1:42 pm
Location: São Paulo, Brasil
Contact:

Re: ValF() speed

Post by Otrebor »

I'm using PB 5.44LTS - Win Vista 32bit. My PC is a bit outdated :wink:

My results are:
ValF() = 840ms
Val = 41ms
Wolfram
Enthusiast
Enthusiast
Posts: 568
Joined: Thu May 30, 2013 4:39 pm

Re: ValF() speed

Post by Wolfram »

Here, on my old MAcBook ValF() is a little bit faster.
But it varies a lot.
macOS Catalina 10.15.7
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: ValF() speed

Post by wilbert »

I guess it's more complicated to convert to float but ValF also needs to recognize more characters like 'e'.

Code: Select all

Debug ValF("1.54e2")
In your case it makes more sense to do it like you are doing or create your own parser not only because of the speed but also because floats are inaccurate.

Code: Select all

A = ValF("1000000.54")*100
Debug A
Windows (x64)
Raspberry Pi OS (Arm64)
Post Reply