Created
December 16, 2020 15:34
-
-
Save Mons/f4d5e70fcc83a3f25f1e3ea7197ccd1e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
local fiber = require 'fiber' | |
local clock = require 'clock' | |
local N = 1e6 | |
do | |
local i = 0; | |
local cond = fiber.cond() | |
local function creator() | |
i = i + 1 | |
if i > N then | |
cond:signal() | |
return | |
end | |
-- print("Executed", i) | |
fiber.new(creator) | |
end | |
local st_t = clock.time64() | |
local st_c = clock.proc64() | |
creator() | |
cond:wait() | |
local t = tonumber(clock.time64() - st_t)/1e9 | |
local c = tonumber(clock.proc64() - st_c)/1e9 | |
print(string.format("%0.6f, %0.6f\n", t, c)) | |
end | |
do | |
do | |
local queue = {} | |
local wakeup = fiber.cond() | |
local worker | |
function fiber.schedule(f,...) | |
table.insert(queue,{f,...}) | |
if not worker then | |
worker = fiber.new(function() | |
while true do | |
while true do | |
local task = table.remove(queue, 1) | |
if not task then break end | |
pcall(unpack(task)) | |
end | |
wakeup:wait() | |
end | |
end) | |
else | |
wakeup:signal() | |
end | |
end | |
end | |
local i = 0; | |
local cond = fiber.cond() | |
local function creator() | |
i = i + 1 | |
if i > N then | |
cond:signal() | |
return | |
end | |
-- print("Executed", i) | |
fiber.schedule(creator) | |
end | |
local st_t = clock.time64() | |
local st_c = clock.proc64() | |
creator() | |
cond:wait() | |
local t = tonumber(clock.time64() - st_t)/1e9 | |
local c = tonumber(clock.proc64() - st_c)/1e9 | |
print(string.format("%0.6f, %0.6f\n", t, c)) | |
end | |
os.exit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
1.390297, 1.265788
0.252121, 0.233272