Last active
May 16, 2022 01:22
-
-
Save LiXizhi/911069b7e7f98db76d295dc7d1c5e34a to your computer and use it in GitHub Desktop.
Luajit 2.1 corountine vs function call comparision
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Testing coroutine overhead in LuaJIT 2.1 with NPL runtime | |
--[[ | |
Starting function test... | |
memory(KB): 0.35546875 | |
Functions: 500000 | |
Elapsed time: 0 s | |
Starting coroutine test... | |
memory(KB): 13781.81640625 | |
Coroutines: 500000 | |
Elapsed time: 0.191 s | |
Starting single coroutine test... | |
memory(KB): 0.4453125 | |
Coroutines: 500000 | |
Elapsed time: 0.02800000000002 | |
conclusions: | |
1. memory overhead: 0.26KB per coroutine | |
2. yield/resume pair overhead: 0.0004 ms | |
if you have 1000 objects each is calling yield/resume at 60FPS, then the time overhead is 0.2*1000/500000*60*1000 = 24ms | |
and if you do not reuse coroutine, then memory overhead is 1000*60*0.26 = 15.6MB/sec | |
]] | |
local total = 500000 | |
local start, stop | |
function loopy(n) | |
n = n + 1 | |
return n | |
end | |
print "Starting function test..." | |
collectgarbage("collect");collectgarbage("collect");collectgarbage("collect"); | |
local beforeCount =collectgarbage("count") | |
start = os.clock() | |
for i = 1, total do | |
loopy(i) | |
end | |
stop = os.clock() | |
print("memory(KB):", collectgarbage("count") - beforeCount) | |
print("Functions:", total) | |
print("Elapsed time:", stop-start, " s") | |
print "Starting coroutine test..." | |
collectgarbage("collect");collectgarbage("collect");collectgarbage("collect"); | |
local beforeCount =collectgarbage("count") | |
start = os.clock() | |
for i = 1, total do | |
co = coroutine.create(loopy) | |
coroutine.resume(co, i) | |
end | |
stop = os.clock() | |
print("memory(KB):", collectgarbage("count") - beforeCount) | |
print("Coroutines:", total) | |
print("Elapsed time:", stop-start, " s") | |
print "Starting single coroutine test..." | |
collectgarbage("collect");collectgarbage("collect");collectgarbage("collect"); | |
local beforeCount =collectgarbage("count") | |
start = os.clock() | |
co = coroutine.create(function() | |
for i = 1, total do | |
loopy(i) | |
coroutine.yield(); | |
end | |
end) | |
for i = 1, total do | |
coroutine.resume(co, i) | |
end | |
stop = os.clock() | |
print("memory(KB):", collectgarbage("count") - beforeCount) | |
print("Coroutines:", total) | |
print("Elapsed time:", stop-start, " s") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment