Given a big string with numbers in it like
input = ";(,)<mul(595,110)~ #(select()-?who():mul(732,729)+/;%@mul(924,700)"
and I know at execution time exactly where a number is in the string, I can parse that section of the string like so
i = 10
parse(Int, input[i:i+2])
but this allocates
Benchmark: 2605 samples with 1264 evaluations
min 19.896 ns (1 allocs: 32 bytes)
median 23.248 ns (1 allocs: 32 bytes)
mean 29.374 ns (1 allocs: 32 bytes, 0.08% gc time)
max 7.418 μs (1 allocs: 32 bytes, 99.26% gc time)
which can (in my experiments) easily add up over a very big string and cause a slow down.
My solution was to just implement a very naive parser that doesn’t allocate
arr = codeunits(input) #outside the call to naiveparseint and used many times
function naiveparseint(arr, xstart, xend)
output::Int = 0
pow = 0
for i in xend:-1:xstart
output += (arr[i]-0x30)*10^pow
pow += 1
end
return output
end
but it feels like there must be a better way to do this in Julia.
Any ideas?