mirror of
https://github.com/littensy/charm.git
synced 2026-10-22 12:41:37 +00:00
refactor: improve logic
This commit is contained in:
parent
210e948225
commit
72f936591e
48 changed files with 434 additions and 349 deletions
48
benches/bench.luau
Normal file
48
benches/bench.luau
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
local profiles: { [string]: number } = {}
|
||||
local times: { [string]: number } = {}
|
||||
local stack: { string } = {}
|
||||
|
||||
local function run(name: string, test: () -> ())
|
||||
local start = os.clock()
|
||||
local iter = 0
|
||||
|
||||
table.clear(profiles)
|
||||
table.clear(times)
|
||||
table.clear(stack)
|
||||
|
||||
while os.clock() - start < 2 do
|
||||
iter += 1
|
||||
test()
|
||||
end
|
||||
|
||||
local elapsed = os.clock() - start
|
||||
local results = {}
|
||||
|
||||
table.insert(results, `{name}: \t{string.format("%.2f", elapsed / iter * 1000 * 1000)} us/iter`)
|
||||
|
||||
for name, elapsed in times do
|
||||
table.insert(results, `{name}: \t{string.format("%.2f", elapsed / iter * 1000 * 1000)} us/iter`)
|
||||
end
|
||||
|
||||
print(table.concat(results, " | "))
|
||||
end
|
||||
|
||||
local function profilebegin(name: string)
|
||||
profiles[name] = os.clock()
|
||||
table.insert(stack, name)
|
||||
end
|
||||
|
||||
local function profileend()
|
||||
local name = table.remove(stack)
|
||||
|
||||
if name then
|
||||
times[name] = (times[name] or 0) + os.clock() - profiles[name]
|
||||
profiles[name] = nil
|
||||
end
|
||||
end
|
||||
|
||||
return {
|
||||
run = run,
|
||||
profilebegin = profilebegin,
|
||||
profileend = profileend,
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue