-
Notifications
You must be signed in to change notification settings - Fork 1
/
Timer.m
120 lines (103 loc) · 3.16 KB
/
Timer.m
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
classdef Timer < handle
properties
depth = 0
next_id = 0
active = true
event = emptyEvent()
spool = emptyEvent()
end
methods ( Static )
function obj = Timer()
persistent localObj;
if ( isempty(localObj) || ~isvalid(localObj) )
localObj = obj;
end
obj = localObj;
end
function tic()
timer = Timer();
e = struct('tic', [], 'toc', [], 'message', [], 'depth', [], 'id', []);
e.tic = tic();
e.depth = timer.depth;
e.id = timer.next_id;
timer.depth = timer.depth + 1;
timer.next_id = timer.next_id + 1;
timer.event(end+1) = e;
end
function toc(message)
timer = Timer();
if ( isempty(timer.event) )
error('toc() has no matching tic().');
end
e = timer.event(end);
e.toc = toc(e.tic);
e.message = message;
timer.spool(end+1) = e;
timer.event(end) = [];
timer.depth = timer.depth - 1;
if ( timer.active && timer.depth == 0 )
timer.next_id = 0;
Timer.print();
end
end
function log(message)
timer = Timer();
e = struct('tic', [], 'toc', [], 'message', [], 'depth', [], 'id', []);
e.message = message;
e.depth = timer.depth;
e.id = timer.next_id;
timer.next_id = timer.next_id + 1;
timer.spool(end+1) = e;
if ( timer.active && timer.depth == 0 )
timer.next_id = 0;
Timer.print();
end
end
function reset()
timer = Timer();
timer.depth = 0;
timer.next_id = 0;
timer.active = true;
timer.event = emptyEvent();
timer.spool = emptyEvent();
end
function on()
timer = Timer();
timer.active = true;
end
function off()
timer = Timer();
timer.active = false;
end
end
methods ( Static, Access = private )
function print()
timer = Timer();
if ( timer.active )
[~, idx] = sort([timer.spool.id]);
for k = 1:length(timer.spool)
e = timer.spool(idx(k));
pre = '';
if ( e.depth > 0 )
pre = [' ' repmat(' ', 1, e.depth-1) '─ '];
end
if ( isempty(e.toc) )
fprintf([pre e.message '\n']);
else
fprintf([pre e.message ': %gs\n'], e.toc);
end
end
timer.spool(1:end) = [];
end
end
end
end
function event = emptyEvent
persistent emptyEvent
if ( isempty(emptyEvent) )
fields = {'tic', 'toc', 'message', 'depth', 'id'};
fields{2,1} = {};
emptyEvent = struct(fields{:});
end
event = emptyEvent;
end