Search code examples
performancejuliaheap-memoryallocation

Parsing a UInt8 Array of chars into a float without allocation


I have a this problem here:

function main()
    b = UInt8[0x35, 0x30, 0x2e, 0x30] # This is "50.0" in bytes
    s = parse(Float64, String(b))
end
@btime main()

250.269 ns (2 allocations: 128 bytes)
50.0

I have a UInt8 array which contains a number in string format e.g. "50.0". I want to parse this number into a float without allocation and as fast as possible (I have millions of these numbers to parse). Is there a better way than the way I've produced above (ignoring the allocation for the UInt8 array since this will not exist).

Cheers guys!


Solution

  • You can use:

    julia> using Parsers
    
    julia> b = UInt8[0x35, 0x30, 0x2e, 0x30] # This is "50.0" in bytes
    4-element Vector{UInt8}:
     0x35
     0x30
     0x2e
     0x30
    
    julia> @btime Parsers.parse(Float64, $b)
      13.527 ns (0 allocations: 0 bytes)
    50.0
    

    Note that your code allocates b inside main so it including the time and memory allocation of creation of b.

    Also the difference is that in your code String(b) empties b, while with Parsers.parse the b is left untouched. This is especially useful if b would be a view of a slice of a longer tape of UInt8 values.