So, I just discovered this:
julia> using BenchmarkTools
julia> @btime parse(Float64, "50.303")
32.163 ns (0 allocations: 0 bytes)
50.303
julia> @btime parse(Float32, "50.303")
71.524 ns (0 allocations: 0 bytes)
50.303f0
julia> @btime convert(Float32, parse(Float64, "50.303"))
32.430 ns (0 allocations: 0 bytes)
50.303f0
If parsing as Float64 and converting to Float32 is twice as fast, why not do it that way by default?
I googled around a bit and couldn't find anything on this so hopefully I'm not missing something obvious here.
My version info:
julia> versioninfo()
Julia Version 1.5.0
Commit 96786e22cc (2020-08-01 23:44 UTC)
Platform Info:
OS: macOS (x86_64-apple-darwin18.7.0)
CPU: Intel(R) Core(TM) i5-8257U CPU @ 1.40GHz
WORD_SIZE: 64
LIBM: libopenlibm
LLVM: libLLVM-9.0.1 (ORCJIT, skylake)
Environment:
JULIA_NUM_THREADS = 4
I also just installed julia fresh on a windows machine and got:
julia> using BenchmarkTools
julia> @btime parse(Float64, "50.303")
530.521ns (0 allocations: 0 bytes)
50.303
julia> @btime parse(Float32, "50.303")
532.105 ns (0 allocations: 0 bytes)
50.303f0
julia> @btime convert(Float32, parse(Float64, "50.303"))
530.521 ns (0 allocations: 0 bytes)
50.303f0
julia> versioninfo()
Julia Version 1.5.1
Commit 697e782ab8 (2020-08-25 20:08 UTC)
Platform Info:
OS: Windows (x86_64-w64-mingw32)
CPU: Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz
WORD_SIZE: 64
LIBM: libopenlibm
LLVM: libLLVM-9.0.1 (ORCJIT, skylake)
So on that machine it seems to make no difference I guess.
AFAICT this calls libc function so that seems to be a property of your libc parse function...
Yes, it does call a libc function. But if it's the case that it's always either faster or just as fast to parse as Float64 and convert, then why not do that?
Quick plug for the Parsers.jl package which has native julia parsing implementations:
julia> @btime Parsers.parse(Float64, "50.303")
13.697 ns (0 allocations: 0 bytes)
50.303
julia> @btime Parsers.parse(Float32, "50.303")
13.997 ns (0 allocations: 0 bytes)
50.303f0
julia> @btime convert(Float32, Parsers.parse(Float64, "50.303"))
13.586 ns (0 allocations: 0 bytes)
50.303f0
Any reason why these aren't in Base
?
It's just a lot of work to get things into base/stdlib, much more than just a PR.
Most helpful comment
Quick plug for the Parsers.jl package which has native julia parsing implementations: