idamnjanovic@1
|
1 function tid = timerinit(par1,par2)
|
idamnjanovic@1
|
2 %TIMERINIT Initialize a new timer.
|
idamnjanovic@1
|
3 % TID = TIMERINIT() initializes a new timer for counting elapsed time,
|
idamnjanovic@1
|
4 % and returns its id.
|
idamnjanovic@1
|
5 %
|
idamnjanovic@1
|
6 % TID = TIMERINIT('TIMERNAME') sets the timer name to the specified
|
idamnjanovic@1
|
7 % string for display purposes.
|
idamnjanovic@1
|
8 %
|
idamnjanovic@1
|
9 % TID = TIMERINIT(ITERNUM) initializes a new ETA timer for a process with
|
idamnjanovic@1
|
10 % ITERNUM iterations. An ETA timer can be used for both counting elapsed
|
idamnjanovic@1
|
11 % time and estimating remaining time.
|
idamnjanovic@1
|
12 %
|
idamnjanovic@1
|
13 % TID = TIMERINIT('TIMERNAME',ITERNUM) sets the ETA timer name to the
|
idamnjanovic@1
|
14 % specified string for display purposes.
|
idamnjanovic@1
|
15 %
|
idamnjanovic@1
|
16 % Example:
|
idamnjanovic@1
|
17 %
|
idamnjanovic@1
|
18 % tid = timerinit(100);
|
idamnjanovic@1
|
19 % for i = 1:100
|
idamnjanovic@1
|
20 % pause(0.07);
|
idamnjanovic@1
|
21 % timereta(tid,i,1);
|
idamnjanovic@1
|
22 % end
|
idamnjanovic@1
|
23 % timereta(tid,i);
|
idamnjanovic@1
|
24 %
|
idamnjanovic@1
|
25 % See also TIMERETA, TIMERCLEAR.
|
idamnjanovic@1
|
26
|
idamnjanovic@1
|
27
|
idamnjanovic@1
|
28 % Ron Rubinstein
|
idamnjanovic@1
|
29 % Computer Science Department
|
idamnjanovic@1
|
30 % Technion, Haifa 32000 Israel
|
idamnjanovic@1
|
31 % ronrubin@cs
|
idamnjanovic@1
|
32 %
|
idamnjanovic@1
|
33 % June 2008
|
idamnjanovic@1
|
34
|
idamnjanovic@1
|
35
|
idamnjanovic@1
|
36 global utiltbx_timer_start_times % start times
|
idamnjanovic@1
|
37 global utiltbx_time_lastdisp % last display times
|
idamnjanovic@1
|
38 global utiltbx_timer_iternums % iteration numbers
|
idamnjanovic@1
|
39 global utiltbx_timer_lastiter % last queried iteration numbers
|
idamnjanovic@1
|
40 global utiltbx_timer_name % timer names
|
idamnjanovic@1
|
41 global utiltbx_timer_callfun % timer calling functions
|
idamnjanovic@1
|
42
|
idamnjanovic@1
|
43
|
idamnjanovic@1
|
44 % parse function arguments %
|
idamnjanovic@1
|
45
|
idamnjanovic@1
|
46 if (nargin==0)
|
idamnjanovic@1
|
47
|
idamnjanovic@1
|
48 iternum = -1;
|
idamnjanovic@1
|
49 timername = '';
|
idamnjanovic@1
|
50
|
idamnjanovic@1
|
51 elseif (nargin==1)
|
idamnjanovic@1
|
52
|
idamnjanovic@1
|
53 if (ischar(par1))
|
idamnjanovic@1
|
54 iternum = -1;
|
idamnjanovic@1
|
55 timername = par1;
|
idamnjanovic@1
|
56
|
idamnjanovic@1
|
57 elseif (isnumeric(par1) && numel(par1)==1 && par1>0)
|
idamnjanovic@1
|
58 iternum = par1;
|
idamnjanovic@1
|
59 timername = '';
|
idamnjanovic@1
|
60
|
idamnjanovic@1
|
61 else
|
idamnjanovic@1
|
62 error('Invalid number of iterations');
|
idamnjanovic@1
|
63 end
|
idamnjanovic@1
|
64
|
idamnjanovic@1
|
65 elseif (nargin==2)
|
idamnjanovic@1
|
66
|
idamnjanovic@1
|
67 if (ischar(par1) && isnumeric(par2))
|
idamnjanovic@1
|
68 if (numel(par2)==1 && par2>0)
|
idamnjanovic@1
|
69 timername = par1;
|
idamnjanovic@1
|
70 iternum = par2;
|
idamnjanovic@1
|
71 else
|
idamnjanovic@1
|
72 error('Invalid number of iterations');
|
idamnjanovic@1
|
73 end
|
idamnjanovic@1
|
74 else
|
idamnjanovic@1
|
75 error('Invalid function syntax');
|
idamnjanovic@1
|
76 end
|
idamnjanovic@1
|
77
|
idamnjanovic@1
|
78 else
|
idamnjanovic@1
|
79 error('Too many function parameters');
|
idamnjanovic@1
|
80 end
|
idamnjanovic@1
|
81
|
idamnjanovic@1
|
82
|
idamnjanovic@1
|
83 % register the timer %
|
idamnjanovic@1
|
84
|
idamnjanovic@1
|
85 if (isempty(utiltbx_timer_start_times))
|
idamnjanovic@1
|
86 utiltbx_timer_start_times = clock;
|
idamnjanovic@1
|
87 utiltbx_time_lastdisp = utiltbx_timer_start_times;
|
idamnjanovic@1
|
88 utiltbx_timer_iternums = double(iternum);
|
idamnjanovic@1
|
89 utiltbx_timer_lastiter = 0;
|
idamnjanovic@1
|
90 utiltbx_timer_name = { timername };
|
idamnjanovic@1
|
91 utiltbx_timer_callfun = {};
|
idamnjanovic@1
|
92 tid = 1;
|
idamnjanovic@1
|
93 else
|
idamnjanovic@1
|
94 utiltbx_timer_start_times(end+1,:) = clock;
|
idamnjanovic@1
|
95 utiltbx_time_lastdisp(end+1,:) = utiltbx_timer_start_times(end,:);
|
idamnjanovic@1
|
96 utiltbx_timer_iternums(end+1) = double(iternum);
|
idamnjanovic@1
|
97 utiltbx_timer_lastiter(end+1) = 0;
|
idamnjanovic@1
|
98 utiltbx_timer_name{end+1} = timername;
|
idamnjanovic@1
|
99 tid = size(utiltbx_timer_start_times,1);
|
idamnjanovic@1
|
100 end
|
idamnjanovic@1
|
101
|
idamnjanovic@1
|
102
|
idamnjanovic@1
|
103 % detect timer calling function %
|
idamnjanovic@1
|
104
|
idamnjanovic@1
|
105 st = dbstack;
|
idamnjanovic@1
|
106 if (length(dbstack) >= 2)
|
idamnjanovic@1
|
107 utiltbx_timer_callfun{end+1} = st(2).name;
|
idamnjanovic@1
|
108 else
|
idamnjanovic@1
|
109 utiltbx_timer_callfun{end+1} = '';
|
idamnjanovic@1
|
110 end
|