-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathLapTimer.lua
58 lines (45 loc) · 1.45 KB
/
LapTimer.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
-- LapTimer.cpu
-- accumulate wall clock and CPU time
require 'ifelse'
-- API overview
if false then
lt = LapTimer()
lt:lap('part 1')
lt:lap('part 2')
for lapname, cpuWallclock in pairs(lt:getTimes()) do
cpu = cpuWallclock.cpu
wallclock = cpuWallclock.wallclock
end
end
require 'Accumulators'
require 'makeVp'
-------------------------------------------------------------------------------
-- CONSTRUCTION
-------------------------------------------------------------------------------
torch.class('LapTimer')
function LapTimer:__init(functionName, fileDescriptor)
self.cpuAccumulators = Accumulators()
self.wallclockAccumulators = Accumulators()
self.timer = torch.Timer() -- starts the timer
end
-------------------------------------------------------------------------------
-- PUBLIC METHODS
-------------------------------------------------------------------------------
function LapTimer:lap(lapname)
local t = self.timer:time() -- returns t.user, t.sys, t.real
self.cpuAccumulators:add(lapname, t.user + t.sys)
self.wallclockAccumulators:add(lapname, t.real)
self.timer:reset()
end
function LapTimer:getTimes()
local result = {}
local cpus = self.cpuAccumulators:getTable()
local wallclocks = self.wallclockAccumulators:getTable()
for lapname, cpu in pairs(cpus) do
result[lapname] = {
cpu = cpu,
wallclock = wallclocks[lapname],
}
end
return result
end