-
Notifications
You must be signed in to change notification settings - Fork 0
/
Benchmark.lua
42 lines (35 loc) · 1.12 KB
/
Benchmark.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
local Benchmark = {}
setmetatable(Benchmark, { __index = Benchmark })
---@param fasterTime number
---@param slowerTime number
---@return number
function Benchmark:calculatePercentage(fasterTime, slowerTime)
local delta = slowerTime - fasterTime
return delta / slowerTime * 100
end
---@param callback function
---@param iteration number
---@return number
function Benchmark:functions(callback, iteration)
iteration = iteration or 1
local totalTime = 0
for i = 0, iteration do
local initialTime = os.clock()
callback()
local finalTime = os.clock()
local deltaTime = finalTime - initialTime
totalTime = totalTime + deltaTime
end
local averageTime = totalTime / iteration
return averageTime
end
---@param averageTime1 (number)
---@param averageTime2 (number)
---@return number
function Benchmark:compare(averageTime1, averageTime2)
local fasterTime = math.min(averageTime1, averageTime2)
local slowerTime = math.max(averageTime1, averageTime2)
local percentage = self:calculatePercentage(fasterTime, slowerTime)
return percentage
end
return Benchmark