on the problem of Tic ToC in MATLAB
One
MATLAB actual unit time timing function of the specific application, when writing programs, often need to know the actual code execution time, which needs to be used in the program of the timing function, MATLAB provides the following three ways:
1.cputime (unit unknown)
Back to the CPU time since the start of MATLAB, you can get the actual running time of the program when the program executes the money to save the CPU time at that time, then, after the program execution, subtract the pre-run values with CPUTime.
>>t0=cputime;pause (3); Timecost=cputime-t0
2.TIC/TOC (unit s)
TIC is used at the beginning of a program to start a timer and then put a TOC at the end of the program, indicating the terminating timer, and returning the total s between the tic start-up
3.etime (unit s)
ETime (T1,T2) is used to calculate the time difference between the two date vectors t1 and T2, combined with the clock function mentioned earlier, to determine how long the program code runs.
>>t0=clock;pause (3); Timecost=etime (CLOCK,T0)
The second type is recommended in three timings, which is relatively accurate. Of course, you can use the profiler to determine your execution time, and the exact time that you don't have a command.
Often we need to calculate how long our program runs so that we can compare the execution efficiency of the program. Of course it doesn't make sense to have a small program for just a few seconds, but it's important for a big program.
Let's talk about the three common methods of calculating program run time in MATLAB!
Note: Three methods due to the use of different principles, the results may be a certain gap!
Second
1. Tic and TOC combination (most used)
Calculates the elapsed time between the TIC and the TOC, and its classic format is
Tic
。。。。。。。。。。
Toc
Copy Code
In other words, when a program encounters a tic, Matlab automatically starts timing, and when it runs to the TOC, it automatically calculates the time between this and the most recent tic. This is a bit of a mouthful, let's give an example
% by dynamic of the MATLAB Technology Forum
% See also http://www.matlabsky.com
% contact me [email protected]
% 2009-08-18 12:08:47
Clc
Tic;%tic1
T1=clock;
For I=1:3
TIC;%tic2
T2=clock;
Pause (3*rand)
% is calculated to the last time the TIC was encountered, in other words, the time of each cycle
DISP ([' TOC calculation ', Num2str (i), ' sub-cycle run time: ', Num2str (TOC)]);
% calculates the time of each cycle
Disp ([' etime calculation ', num2str (i), ' sub-cycle run time: ', Num2str (ETime (CLOCK,T2))]);
% Calculation Program Total run time
Disp ([' etime calculation program from start to current run time: ', Num2str (ETime (CLOCK,T1))]);
Disp (' ====================================== ')
End
% calculates the time to Tic2, because the last time the TIC was encountered in the For Loop's i=3, so the last loop was calculated
DISP ([' TOC calculation last cycle run time ', Num2str (TOC)])
Disp (total running time of the [' ETime program: ', Num2str (ETime (CLOCK,T1))]);
Copy Code
The results of the operation are as follows, you can analyze yourself
TOC calculation 1th cycle run time: 2.5628
ETime calculation of the 1th cycle run time: 2.562
ETime calculation program from start to now run time: 2.562
======================================
TOC calculation 2nd cycle run time: 2.8108
ETime calculation of the 2nd cycle run time: 2.813
ETime calculation program from start to now run time: 5.375
======================================
TOC calculation 3rd cycle run time: 2.0462
ETime calculation of the 3rd cycle run time: 2.046
ETime calculation program from start to now run time: 7.421
======================================
TOC calculation last cycle run time 2.0479
ETime Total program run time: 7.421
Copy Code
2, ETime (T1,T2) and clock coordination
To calculate the time difference between t1,t2, which is calculated by invoking the clock of the Windows system to calculate the elapsed times, the form of the application
T1=clock;
。。。。。。。。。。。
T2=clock;
ETime (T2,T1)
Copy Code
As for the example, I won't give it up because the ETime function is used in the example above.
3, CPUTime function to complete
The use of the method and etime similar, but this is the CPU using the main frequency calculation, and the previous principle is different, using the format as follows
T0=cputime
。。。。。。。。。。。。。
T1=cputime-t0
Copy Code
There are three ways to calculate the program run time, but MATLAB is the official recommended use of TIC/TOC combination, when timing the duration of an event, using the TIC and TOC functions Inst EAD of clock or etime.
As for everyone can choose according to their own preferences, but when using TIC/TOC must be aware that the TOC is calculated with the last run of the tic between the time, not the first tic, not the second one ....
--------------------------------------------------------------------------------------------------------------- --------------------------------------------------------
TIC begins a stopwatch timer. By TIC, you can get the time spent using TIC commands to use the TOC command
Tstart=tic save time as an output parameter the value of Tstart.tstart is useful only as an input parameter to the TOC call that appears later.
For example: Measure the minimum and average time for the Bessel function:
REPS = 1000; Mintime = INF; Nsum = 10;
Tic
For I=1:reps
Tstart = tic;
sum = 0; For j=1:nsum, sum = sum + BesselJ (j,reps); End
telapsed = TOC (Tstart);
mintime = min (telapsed,mintime);
book.iLoveMatlab.cn
End
Averagetime = Toc/reps;
--------------------------------------------------------------------------------------------------------------- -----------------------------------------------------------
How do I store tic toc processed in a matrix in chronological order? Because I found that each processing will overwrite the last time and I need to record the time of each processing to hope that a master can help guide.
You can assign a matrix first, and then save the value every time you run it.
A=ones (m,n)% of which m,n is the number of times you handle;
For I=1..num
Tic
% of your program;
A=toc;
A (i) =a;
End
So the last time is in a.
On the problem of Tic ToC in matlab