Implement profiler

This commit is contained in:
2026-06-20 14:21:26 +03:00
parent 45a09284c2
commit ab48b57e1b
10 changed files with 294 additions and 17 deletions
+35
View File
@@ -0,0 +1,35 @@
local thing = _G._PUBLIC or _G
thing.__PROFILER_INSTANCE = thing.__PROFILER_INSTANCE or { timers = {} }
local timers = thing.__PROFILER_INSTANCE.timers
local profiler = {}
function profiler.start(label, overwrite)
thing.__PROFILER_INSTANCE.lastadded = label
timers[label] = timers[label] or {}
if not timers[label].start or overwrite then
timers[label].start = os.clock()
end
return function() timers[label].time = timers[label].time or os.clock() - timers[label].start end
end
function profiler.results()
local _now = nil
local function now()
_now = _now or os.clock()
return _now
end
local out = {}
for label, t in pairs(timers) do
table.insert(out, { label = label, time = t.time or now() - t.start })
end
table.sort(out, function(a, b) return a.time > b.time end)
return out
end
function profiler.profile(label, func, ...)
local stop = profiler.start(label)
func(...)
stop()
end
return profiler