Lua string to number parsing speed optimization

980 views Asked by At

I am trying to make a speedtest using Lua as one of the languages and I just wanted some advice on how I could make my code a bit faster if possible. It is important that I do my own speedtest, since I am looking at very specific parameters.

The code is reading from a file which looks something like this, but the numbers are randomly generated and range from 1 zu 1 000 000. There are between 100 and 10 000 numbers in one list:

type
(123,124,364,5867,...)

type
(14224,234646,5686,...)

...

The type is meant for another language, so it can be ignored. I just put this here so you know why I am not parsing every line. This is my Lua code:

incr = 1
for line in io.lines(arg[1]) do
  incr = incr +1
  if incr % 3 == 0 then
    line:gsub('([%d]+),?',function(n)tonumber(n)end)
  end
end

Now, the code works and does exactly what I want it to do. This is not about getting it to work, this is simply about speed. I need ideas and advice to make the code work at optimal speed.

Thanks in advance for any answers.

1

There are 1 answers

5
Egor Skriptunoff On BEST ANSWER

IMHO, this tonumber() benchmarking is rather strange. Most of CPU time would be spent on other tasks (regexp parsing, file reading, ...).

Instead of converting to number and ignoring result it would be more logical to calculate sum of all the numbers in input file:

local gmatch, s = string.gmatch, 0

for line in io.lines(arg[1]) do
  for n in gmatch(line, '%d+') do
    s = s + n  -- converting string to number is automatic here
  end
end

print(s)