Like if you need to create a grid. The bigger it is, the longer it takes to generate. But you shouldn't yield every single time in the loop because you end up making something that might take a few seconds into a half a minute or more. So you only yield every few hundred or thousand or so.
For example:
Code: Select all
function love.load()
--Create a 5000x5000 grid
local t = love.timer.getTime()
grid = {}
for x = 1, 5000 do
grid[x] = {}
for y = 1, 5000 do
grid[x][y] = 1
end
end
print("Time for non Coroutine Grid", love.timer.getTime() - t)
end
Code: Select all
function love.load()
--Set it up for coroutine this time
total = 0
grid = {}
co = coroutine.create(function()
local t = love.timer.getTime()
local i = 0
for x = 1, 5000 do
grid[x] = {}
for y = 1, 5000 do
grid[x][y] = 1
total = total + 1
i = i + 1
if i >= 10000 then
coroutine.yield()
i = 0
end
end
end
print("Time for Coroutine Grid", love.timer.getTime() - t)
end)
end
function love.update(dt)
coroutine.yield(co)
end
function love.draw()
love.graphics.print(love.graphics.getFPS() .. "\n" .. total, 10, 10)
end
For instance:
Code: Select all
1000 per call:
Time for non Coroutine Grid 0.385
Time for Coroutine Grid 45.052
10000 per call:
Time for non Coroutine Grid 0.385
Time for Coroutine Grid 4.329
100000 per call:
Time for non Coroutine Grid 0.427
Time for Coroutine Grid 0.93
Kind of makes me wish stuff loaded slower so I could make really cool loading screens.