I'm curious as to what might be causing the difference in runtimes. Please see the comments below. As indicated, declaring "x" as local causes somewhere between 1.5-1.8 times slow down for the coroutine loop compared to the function calls, whereas they have nearly identical times otherwise. I am running this on an AMD Phenom II X4 920 machine, with the latest LuaJIT 2.0 source from the development branch. Am I missing something something obvious about LuaJIT coroutines that explains this?
-- Testing coroutine overhead in LuaJIT 2.0
local total = 50000
local start, stop
-- Here's the fun part. The following (global) declaration:
-- x = 0
-- comes with no strings attached; it has the same performance as not declaring
-- x at all (as expected), or as declaring local x in loopy().
-- However, the following declaration:
-- local x = 0
-- causes the coroutine version to be anywhere between 1.5-1.8x slower than the
-- function call. The computer is a
function loopy(n)
x = 0
for i = 1, n do
x = x+i
end
end
--print(0.13/0.07) -- n = 10000, r ~ 1.86
--print(0.48/0.29) -- n = 20000, r ~ 1.66
--print(1.04/0.64) -- n = 30000, r ~ 1.63
--print(1.82/1.16) -- n = 40000, r ~ 1.57
--print(2.89/1.79) -- n = 50000, r ~ 1.61
print "Starting coroutine test..."
start = os.clock()
for i = 1, total do
co = coroutine.create(loopy)
coroutine.resume(co, i)
end
stop = os.clock()
print("Coroutines:", total)
print("Elapsed time:", stop-start, " s")
print "Starting function test..."
start = os.clock()
for i = 1, total do
loopy(i)
end
stop = os.clock()
print("Functions:", total)
print("Elapsed time:", stop-start, " s")