Page 1 of 1

ValF() speed

Posted: Sat Jun 24, 2017 7:51 pm
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))

Re: ValF() speed

Posted: Sat Jun 24, 2017 8:58 pm
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?

Re: ValF() speed

Posted: Sat Jun 24, 2017 9:09 pm
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

Re: ValF() speed

Posted: Sat Jun 24, 2017 9:18 pm
by Wolfram
Here, on my old MAcBook ValF() is a little bit faster.
But it varies a lot.

Re: ValF() speed

Posted: Sun Jun 25, 2017 5:36 am
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