use*_*601 1 performance allocation heap-memory julia
我在这里有一个这个问题:
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
Run Code Online (Sandbox Code Playgroud)
我有一个 UInt8 数组,其中包含一个字符串格式的数字,例如“50.0”。我想将这个数字解析为一个浮点数而不分配并且尽可能快(我有数百万个这些数字要解析)。有没有比我上面产生的方法更好的方法(忽略 UInt8 数组的分配,因为它不存在)。
干杯伙计们!
您可以使用:
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
Run Code Online (Sandbox Code Playgroud)
请注意,您的代码b在内部分配,main因此它包括创建b.
另外,不同的是,在你的代码String(b)清空b,同时与Parsers.parse该b被保持不变。如果b是一段较长的UInt8值带的视图,这尤其有用。