From: http://apps.hi.baidu.com/share/detail/47158063
Three methods for calculating program running time in MATLAB
We often need to calculate how long our program runs, so as to compare the execution efficiency of the program. Of course, this is meaningless for small programs that only have a few seconds, but it is very important for large programs.
Next, let's talk about three common methods for calculating the program running time in MATLAB!
Note:The three methods may have different results due to different usage principles!
1. Combination of TiC and TOC (most used)
Calculate the running time between the program TiC and TOC. Its classic format is
- TIC
- ..........
- TOC
Copy code
In other words, when the program encounters tic, Matlab automatically starts timing and runs to TOC automatically calculates the time between this time and the last tic. This is a bit difficult. The following is an example.
- % By dynamic of MATLAB Technology Forum
- % See also http://www.matlabsky.com
- % Contact me matlabsky@gmail.com
- % 2009-08-18 12:08:47
- CLC
- TIC; % tic1
- T1 = clock;
- For I = 1:3
- TIC; % tic2
- T2 = clock;
- Pause (3 * rand)
- % Calculates the time of the last tic, that is, the time of each loop.
- Disp (['toc calculate the nth ', num2str (I), 'cycle runtime:', num2str (TOC)]);
- % Calculate the time of each loop
- Disp (['etime calculated nth ', num2str (I), 'cycle runtime:', num2str (etime (clock, T2)]);
- % Total running time of the computing program
- Disp (['time when the etime computing program starts to run: ', num2str (etime (clock, T1)]);
- Disp ('========================================== = ')
- End
- % Calculate the time from this time to tic2. Because TiC is at I = 3 of the for loop, the time of the last loop is calculated.
- Disp (['toc calculate the last cyclic runtime ', num2str (TOC)])
- Disp (['total running time of the etime program: ', num2str (etime (clock, T1)]);
Copy code
The running result is as follows. You can analyze it yourself.
- 1st running times of TOC computing: 2.5628
- Etime: 1st running cycles: 2.562
- The time for running the etime computing program from the start to the current: 2.562
- ==============================================
- 2nd running times of TOC computing: 2.8108
- Etime: 2nd running cycles: 2.813
- The time for running the etime computing program from the start to the current: 5.375
- ==============================================
- 3rd running times of TOC computing: 2.0462
- Etime: 3rd running cycles: 2.046
- The time for running the etime computing program from the start to the current: 7.421
- ==============================================
- The last running time of TOC computing is 2.0479.
- Total running time of the etime program: 7.421
Copy code
2. The etime (T1, T2) is used with clock.
To calculate the time difference between t1 and t2. It is obtained by calling the Windows system clock for time difference calculation.
- T1 = clock;
- ...........
- T2 = clock;
- Etime (t2, T1)
Copy code
As for the example, I will not give it, because the etime function is used in the above example.
3. Complete the cputime function.
The usage method is similar to etime, but it is calculated based on the CPU clock speed. Unlike the previous principle, the usage format is as follows:
- T0 = cputime
- .............
- T1 = cputime-t0
Copy code
The above three methods can be used to calculate the program running time, But MATLAB officially recommends TiC/TOC combination, when timing the duration of an event, use the TIC and TOC functions instead of clock or etime.
You can choose your preferred option, but when using TiC/TOC, you must note that TOC calculates the time between TiC and the last running tic, not the first tic, not the second .....