summaryrefslogtreecommitdiff
path: root/lua/timer.lua
blob: 2c54ca8f75fb8ce125e61959c33d3e06f0250433 (plain) (blame)
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
local Timer = nerv.class("nerv.Timer")

function Timer:__init()
    self.last = {}
    self.rec = {}
end

function Timer:tic(item)
    self.last[item] = os.clock()
end

function Timer:toc(item)
    if (self.last[item] == nil) then
        nerv.error("item not there")
    end
    if (self.rec[item] == nil) then
        self.rec[item] = 0
    end
    self.rec[item] = self.rec[item] + os.clock() - self.last[item]
end

function Timer:check(item)
    if self.rec[item]==nil then
        nerv.error('item not there')
    end
    nerv.printf('"%s" lasts for %f secs.\n',item,self.rec[item])
end

function Timer:flush()
    for key, value in pairs(self.rec) do
        self.rec[key] = nil
    end
end