performance - Lua string to number parsing speed optimization -
i trying make speedtest using lua 1 of languages , wanted advice on how make code bit faster if possible. important own speedtest, since looking @ specific parameters.
the code reading file looks this, numbers randomly generated , range 1 zu 1 000 000. there between 100 , 10 000 numbers in 1 list:
type (123,124,364,5867,...) type (14224,234646,5686,...) ...
the type
meant language, can ignored. put here know why not parsing every line. lua code:
incr = 1 line in io.lines(arg[1]) incr = incr +1 if incr % 3 == 0 line:gsub('([%d]+),?',function(n)tonumber(n)end) end end
now, code works , want do. not getting work, speed. need ideas , advice make code work @ optimal speed.
thanks in advance answers.
imho, tonumber()
benchmarking rather strange. of cpu time spent on other tasks (regexp parsing, file reading, ...).
instead of converting number , ignoring result more logical calculate sum of numbers in input file:
local gmatch, s = string.gmatch, 0 line in io.lines(arg[1]) n in gmatch(line, '%d+') s = s + n -- converting string number automatic here end end print(s)
Comments
Post a Comment