Description
Running 10000000 loops in a Lua module script using LuaJIT 2.1.0-beta3 in the terminal gives 10.62 seconds.
Using the same module in Defold by requiring the same module gives execution time of 3 minutes.
Can we know what is causing this and if there is any way we can get around this? This is very crucial for our project and we would appreciate any effort from anyone answering this question.
To Reproduce
Steps to reproduce the behavior:
Make a Lua module with following script:
local M = {}
local function run_loop()
for i = 1, 10000000 do
print(i)
end
end
run_loop()
return M
Run it in terminal using “luajit .lua” and evaluate time taken.
Require same Lua module in main.script in an empty Mobile Game template Defold project and evaluate again.
I did as you said. I put a simple 1 + 1 inside the loop and removed the print as so. I even added a simple execution time calculator.
local M = {}
local function run_loop()
local time_start = os.time()
for i = 1, 100000000 do
local a = 1 + 1
end
print("End time: ", os.time() - time_start)
end
run_loop()
return M
The time taken to execute has reduced by a lot.
LuaJIT through terminal → 0 second (probably has float value, but it was very fast)
Defold → 35 seconds
But as you can see, the issue still persists. Execution in Defold is taking a lot more time.
I guess switching of JIT reduces start up time for your test.
Your test is a good first step towards thinking about profiling. Of itself the test is not useful, what is actually being tested is opaque (a compiler might replace 1+1 with 2 or even remove the loop completely, who knows). Then there is relating ‘results’ to what you really want to do. Better to profile your actual game: