annotate Problems/private/timerinit.m @ 44:2c59257d734c

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