Advertisement
Rochet2

Untitled

Mar 26th, 2020
630
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.26 KB | None | 0 0
  1. local tasks = {}
  2.  
  3. function AddTask(f)
  4.   local c = coroutine.create(f)
  5.   tasks[#tasks+1] = c
  6. end
  7.  
  8. local n = 4
  9. local N = 100000
  10.  
  11. local yield = coroutine.yield
  12. for i = 1, N do
  13.     AddTask(function()
  14.         --local yield_att= 0
  15.         local function fibonacci(n)
  16.             yield_att = yield_att +1
  17.             if yield_att % 10000 == 0 then
  18.                 yield()
  19.             end
  20.             if n<3 then
  21.                 return 1
  22.             else
  23.                 return fibonacci(n-1) + fibonacci(n-2)
  24.             end
  25.         end
  26.         --local s = os.clock()
  27.         -- print("DONE", fibonacci(n), os.clock()-s, yields, yield_att)
  28.     end)
  29. end
  30.  
  31. local s = os.clock()
  32. local task = 0
  33. print("STARTING")
  34. local resume = coroutine.resume
  35. local status = coroutine.status
  36. local remove = table.remove
  37. while #tasks > 0 do
  38.   task = (task % #tasks) + 1
  39.   -- print(task)
  40.   resume(tasks[task])
  41.   if status(tasks[task]) == "dead" then
  42.     remove(tasks, task)
  43.   end
  44.   -- FetchNewTasksFromCpp()
  45. end
  46. print("SUMM", os.clock()-s, (os.clock()-s)/N)
  47.  
  48. print("STARTING")
  49. function fibonacci(n)
  50.     if n<3 then
  51.         return 1
  52.     else
  53.         return fibonacci(n-1) + fibonacci(n-2)
  54.     end
  55. end
  56. local s = os.clock()
  57. print("NOYI", fibonacci(n), os.clock()-s)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement