Created
August 27, 2023 18:28
-
-
Save michlbro/e8f1a7db73d5ab458e5bb2e275d25421 to your computer and use it in GitHub Desktop.
Profiler for Lua
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
local Profiler = {} | |
function Profiler:AddTask(taskName, func) | |
self.task[taskName] = {func} | |
end | |
function Profiler:RunTasks(repeats, count) | |
local results = {} | |
for taskName, task in self.task do | |
local func = task[1] | |
if not results[taskName] then | |
results[taskName] = {} | |
end | |
for _ = 1, repeats do | |
local start = os.clock() | |
for _ = 1, count do | |
func() | |
end | |
local endTime = os.clock() | |
table.insert(results[taskName], endTime - start) | |
end | |
end | |
local stringBuffer = "\n" | |
for taskName, result in results do | |
stringBuffer ..= `{taskName}:\n` | |
local addedTime = 0 | |
for repeats, timeTaken in result do | |
addedTime += timeTaken | |
stringBuffer ..= ` [{repeats}]: {timeTaken}s\n` | |
end | |
stringBuffer ..= `\n Average: {addedTime/#result}s\n\n` | |
end | |
return stringBuffer | |
end | |
local function new() | |
local self = {} | |
self.task = {} | |
setmetatable(self, { | |
__index = Profiler | |
}) | |
return self | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment