Note:
Due to the different principles of use of the three methods, there may be a certain gap in the results obtained!
1. Combination of tic and toc (the most used)
In other words, when the program encounters tic, Matlab automatically starts timing, and when it runs to toc, it automatically calculates the time between this time and the latest tic. This is a bit of a mouthful, so let's take an example to illustrate
The running results are as follows, you can analyze it yourself
2. etime(t1, t2) is used with clock
to calculate the time difference between t1 and t2. It calculates the running time by calling the clock of the windows system to calculate the time difference.
3. The usage method of cputime function is
similar to etime, but this is calculated by using the main frequency of cpu, which is different from the previous principle. The usage format is as follows
Matlab officially recommends using the tic/toc combination , When timing the duration of an event, use the tic and toc functions instead of clock or etime.
As for everyone, you can choose according to your own preferences, but you must pay attention when using tic/toc, toc calculates the time between the last tic run , not the first tic, not the second. . . . .
1. Combination of tic and toc (the most used)
Calculate the running time between the program between tic and toc, its classical format is
- tic
- 。。。。。。。。。。
- heel
In other words, when the program encounters tic, Matlab automatically starts timing, and when it runs to toc, it automatically calculates the time between this time and the latest tic. This is a bit of a mouthful, so let's take an example to illustrate
- % by dynamic of Matlab Technical 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
- tick;%tick2
- t2=clock;
- pause(3*margin)
- % Calculate the time until the last time tic was encountered, in other words, the time of each loop
- disp(['toc calculates the first ',num2str(i),' loop running time: ',num2str(toc)]);
- % Calculate the time of each loop
- disp(['etime calculates the first', num2str(i), 'the running time of the loop: ', num2str(etime(clock, t2))]);
- % Calculate the total running time of the program
- disp(['etime calculates the running time of the program from the beginning to the present: ',num2str(etime(clock,t1))]);
- disp ('======================================')
- end
- % Calculate the time from this time to tic2. Since the last time tic is encountered is when i=3 of the for loop, the time of the last loop is calculated
- disp(['toc calculates the last loop running time', num2str(toc)])
- disp(['etime program total running time:',num2str(etime(clock,t1))]);
The running results are as follows, you can analyze it yourself
- toc calculates the running time of the first loop: 2.5628
- etime calculates the running time of the first loop: 2.562
- etime calculates the time the program has run since it started: 2.562
- ======================================
- toc calculates the running time of the 2nd loop: 2.8108
- etime calculates the running time of the 2nd loop: 2.813
- etime calculates the time the program has run since it started: 5.375
- ======================================
- toc calculates the 3rd loop running time: 2.0462
- etime calculates the 3rd loop running time: 2.046
- etime calculates the running time of the program from the beginning to the present: 7.421
- ======================================
- toc calculates the last loop run time 2.0479
- Total etime program running time: 7.421
2. etime(t1, t2) is used with clock
to calculate the time difference between t1 and t2. It calculates the running time by calling the clock of the windows system to calculate the time difference.
- t1=clock;
- 。。。。。。。。。。。
- t2=clock;
- etime(t2,t1)
3. The usage method of cputime function is
similar to etime, but this is calculated by using the main frequency of cpu, which is different from the previous principle. The usage format is as follows
- t0 = cputime
- 。。。。。。。。。。。。。
- t1 = cputime-t0
Matlab officially recommends using the tic/toc combination , When timing the duration of an event, use the tic and toc functions instead of clock or etime.
As for everyone, you can choose according to your own preferences, but you must pay attention when using tic/toc, toc calculates the time between the last tic run , not the first tic, not the second. . . . .
Reprinted from: http://www.matlabsky.com/thread-2607-1-1.html