Skip to content

Instantly share code, notes, and snippets.

@jamesreiati
Last active April 19, 2022 00:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesreiati/543936efb8a48c1b22e7932fd10f8b0c to your computer and use it in GitHub Desktop.
Save jamesreiati/543936efb8a48c1b22e7932fd10f8b0c to your computer and use it in GitHub Desktop.
Multi-Return vs Table on Playdate Simulator (Runtime 1.10.0)
print(playdate.apiVersion())
local countString <const> = "count"
local function returnSingle()
return 4
end
local function returnMultival()
return 4, 5, 6
end
local function returnTable()
return { x = 4, y = 5, z = 6}
end
local function printArray(title, array)
print(title)
for index, value in ipairs(array) do
print(index, value)
end
end
local frameCount = 0
local preFunctionMemory = {-1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0}
local singleFunctionMemory = {-1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0}
local multiFunctionMemory = {-1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0}
local tableFunctionMemory = {-1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0}
local postFunctionMemory = {-1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0}
function playdate.update()
frameCount += 1
if frameCount <= 10 then
preFunctionMemory[frameCount] = collectgarbage(countString)
end
if frameCount == 11 then
for i = 1, 11 do
local a = returnSingle()
singleFunctionMemory[i] = collectgarbage(countString)
end
for i = 1, 11 do
local x, y, z = returnMultival()
local a = x + y + z
multiFunctionMemory[i] = collectgarbage(countString)
end
for i = 0, 11 do
local point = returnTable()
local a = point.x + point.y + point.z
tableFunctionMemory[i] = collectgarbage(countString)
end
end
if frameCount >= 12 and frameCount < 16 then
postFunctionMemory[frameCount - 11] = collectgarbage("count")
end
if frameCount == 16 then
printArray("pre", preFunctionMemory)
printArray("single", singleFunctionMemory)
printArray("multi", multiFunctionMemory)
printArray("table", tableFunctionMemory)
printArray("post", postFunctionMemory)
end
end
17:07:06: 11000 10000
17:07:06: Loading: OK
17:07:07: pre
17:07:07: 1 89.5752
17:07:07: 2 87.77637
17:07:07: 3 87.77637
17:07:07: 4 87.77637
17:07:07: 5 87.77637
17:07:07: 6 87.77637
17:07:07: 7 87.77637
17:07:07: 8 87.77637
17:07:07: 9 87.77637
17:07:07: 10 87.77637
17:07:07: 11 -1.0
17:07:07: 12 -1.0
17:07:07: 13 -1.0
17:07:07: single
17:07:07: 1 87.77637
17:07:07: 2 87.77637
17:07:07: 3 87.77637
17:07:07: 4 87.77637
17:07:07: 5 87.77637
17:07:07: 6 87.77637
17:07:07: 7 87.77637
17:07:07: 8 87.77637
17:07:07: 9 87.77637
17:07:07: 10 87.77637
17:07:07: 11 87.77637
17:07:07: 12 -1.0
17:07:07: 13 -1.0
17:07:07: multi
17:07:07: 1 87.77637
17:07:07: 2 87.77637
17:07:07: 3 87.77637
17:07:07: 4 87.77637
17:07:07: 5 87.77637
17:07:07: 6 87.77637
17:07:07: 7 87.77637
17:07:08: 8 87.77637
17:07:08: 9 87.77637
17:07:08: 10 87.77637
17:07:08: 11 87.77637
17:07:08: 12 -1.0
17:07:08: 13 -1.0
17:07:08: table
17:07:08: 1 88.14355
17:07:08: 2 88.29199
17:07:08: 3 88.44043
17:07:08: 4 88.58887
17:07:08: 5 88.7373
17:07:08: 6 88.88574
17:07:08: 7 89.03418
17:07:08: 8 89.18262
17:07:08: 9 89.33105
17:07:08: 10 89.47949
17:07:08: 11 89.62793
17:07:08: 12 -1.0
17:07:08: 13 -1.0
17:07:08: post
17:07:08: 1 87.84668
17:07:08: 2 87.84668
17:07:08: 3 87.84668
17:07:08: 4 87.84668
17:07:08: 5 -1.0
17:07:08: 6 -1.0
17:07:08: 7 -1.0
17:07:08: 8 -1.0
17:07:08: 9 -1.0
17:07:08: 10 -1.0
17:07:08: 11 -1.0
17:07:08: 12 -1.0
17:07:08: 13 -1.0
Memory allocated for each single return (in Kb, excluding the first): ALL 0
Memory allocated for each multi return (in Kb, excluding the first): ALL 0
Memory allocated for each table (in Kb, excluding the first):
0.14844
0.14844
0.14844
0.14843
0.14844
0.14844
0.14844
0.14843
0.14844
0.14844
AVG: 0.148438
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment