pgimeno wrote: ↑Fri Feb 05, 2021 7:34 pm
Not sure where the difference lies, but it's clear that the "lab test" gives quite different results than my "field test".
Obviously. Seems to be a JIT(?) quirk with the strip_strings_and_comments function, not generally slower performance of string.sub.
Out of curiosity, I removed code from your test until the performance difference vanished.
This code is slow:
Code: Select all
local function strip_strings_and_comments(code)
local i = 1
local len = #code
while i <= len do
local c = code:sub(i, i)
if c == '"' or c == "'" then
elseif c == "[" then
end
i = i + 1
end
return code
end
This code is fast (elseif line removed):
Code: Select all
local function strip_strings_and_comments(code)
local i = 1
local len = #code
while i <= len do
local c = code:sub(i, i)
if c == '"' or c == "'" then
end
i = i + 1
end
return code
end
In this slightly modified test from before, the code using :sub is actually 15% faster than :byte:
Code: Select all
local str = love.filesystem.read('main.lua')
local n = 0
local s = love.timer.getTime()
for j = 1, 1e7 do
for i = 1, #str do
local a = str:byte(i)
if i == 1 then n = n + 1 end
-- if i == 1 then n = n + 1 end
end
end
print("byte", love.timer.getTime() - s)
local n = 0
local s = love.timer.getTime()
for j = 1, 1e7 do
for i = 1, #str do
local a = str:sub(i, i)
if i == 1 then n = n + 1 end
-- if i == 1 then n = n + 1 end
end
end
print("sub", love.timer.getTime() - s)
Uncommenting the second condition in the inner loop makes the runtime explode to almost 9 times slower for the :sub code, while the :byte variant only gets marginally slower. LuaJIT is weird.