-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathLapTimer_test.lua
68 lines (51 loc) · 1.74 KB
/
LapTimer_test.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
59
60
61
62
63
64
65
66
67
-- LapTimer_test.lua
-- unit test
require 'makeVp'
require 'LapTimer'
require 'printTableValue'
require 'Timer'
local vp = makeVp(0, 'tester')
local lt = LapTimer()
lt:lap('one')
lt:lap('two')
lt:lap('one')
local times = lt:getTimes()
for lapname, times in pairs(lt:getTimes()) do
vp(2, 'lapname', lapname, 'times', times)
assert(lapname == 'one' or lapname == 'two')
assert(times.cpu >= 0)
assert(times.wallclock >= 0)
end
if false then
print('determining overhead in the timing mechanism')
local function doNothing(nIterations)
for i = 1, nIterations do
end
end
local justLapLapTimer = LapTimer()
local function justLap(nIterations)
for i = 1, nIterations do
justLapLapTimer:lap('justLap')
end
end
local function getTimes(f, nIterations)
local timer = Timer()
f(nIterations)
local cpu, wallclock = timer:cpuWallclock()
return {cpu = cpu, wallclock = wallclock}
end
local nIterations = 100000
print('nIterations', nIterations)
print('average times per iteration')
local nothingTimes = getTimes(doNothing, nIterations)
printTableValue('nothingTimes', nothingTimes)
print('nothingTimes', nothingTimes)
local nothingCpu = nothingTimes.cpu / nIterations
local nothingWallclock = nothingTimes.wallclock / nIterations
print(string.format('do nothing: cpu %17.15f wallclock %17.15f', nothingCpu, nothingWallclock))
local justLapTimes = getTimes(justLap, nIterations)
local justLap cpu = justLapTimes.cpu / nIterations - nothingCpu
local justLapWallclock = justLapTimes.wallclock / nIterations - nothingWallclock
print(string.format('just lap : cpu %17.15f wallclock %17.15f', nothingCpu, nothingWallclock))
end
print('ok LapTimer')