Files
simple-makes-complex/mods/SYSTEM/Profiler/init.lua

56 lines
1.3 KiB
Lua

local getInstance = Class.getInstance
local getUsTime = core.get_us_time
---@alias Profiler.TimeUnits 'sec'|'us'
---@class Profiler
---@field name string
---@field startTime number
---@field endTime number
---@field deltaTime number
Profiler = {
timers = {}
}
---@param name string?
---@return Profiler
function Profiler:new(name)
local instance = getInstance(self, {})
instance.name = name or 'NoName'
return instance
end
---Marks the time when the timer starts.
function Profiler:startTimer()
self.startTime = getUsTime()
self.deltaTime = nil
self.endTime = nil
end
---Marks the end time of the timer.
function Profiler:endTimer()
self.endTime = getUsTime()
self.deltaTime = self.endTime - self.startTime
end
---Returns the time difference between the start and stop of the timer in seconds (`sec`) or microseconds (`us`).
---@param units Profiler.TimeUnits? Units of time measurement. Default 'us'.
function Profiler:getDeltaTime(units)
if units == nil then
units = 'us'
end
if units == 'us' then
return self.deltaTime
elseif units == 'sec' then
return self.deltaTime/1000000
end
end
---Displays the time difference in the chat using `core.log`.
function Profiler:logDeltaTime()
core.log(('DeltaTime[%s]: %.3f'):format(self:getDeltaTime()))
end