Three Methods of Calculating the Running Time of Program in Matlab

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)

Calculate the running time between the program between tic and toc, its classical format is

[plain] view plain copy
  1. tic  
  2. 。。。。。。。。。。  
  3. 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

[plain] view plain copy
  1. % by dynamic of Matlab Technical Forum  
  2. % see also http://www.matlabsky.com  
  3. % contact me [email protected]  
  4. % 2009-08-18 12:08:47   
  5. clc  
  6. tic;%tic1  
  7. t1=clock;  
  8. for i=1:3  
  9.     tick;%tick2  
  10.     t2=clock;  
  11.     pause(3*margin)  
  12.     % Calculate the time until the last time tic was encountered, in other words, the time of each loop  
  13.     disp(['toc calculates the first ',num2str(i),' loop running time: ',num2str(toc)]);  
  14.     % Calculate the time of each loop  
  15.     disp(['etime calculates the first', num2str(i), 'the running time of the loop: ', num2str(etime(clock, t2))]);  
  16.     % Calculate the total running time of the program  
  17.     disp(['etime calculates the running time of the program from the beginning to the present: ',num2str(etime(clock,t1))]);  
  18.     disp ('======================================')  
  19. end  
  20. % 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  
  21. disp(['toc calculates the last loop running time', num2str(toc)])  
  22. disp(['etime program total running time:',num2str(etime(clock,t1))]);  

The running results are as follows, you can analyze it yourself

[plain] view plain copy
  1. toc calculates the running time of the first loop: 2.5628  
  2. etime calculates the running time of the first loop: 2.562  
  3. etime calculates the time the program has run since it started: 2.562  
  4. ======================================  
  5. toc calculates the running time of the 2nd loop: 2.8108  
  6. etime calculates the running time of the 2nd loop: 2.813  
  7. etime calculates the time the program has run since it started: 5.375  
  8. ======================================  
  9. toc calculates the 3rd loop running time: 2.0462  
  10. etime calculates the 3rd loop running time: 2.046  
  11. etime calculates the running time of the program from the beginning to the present: 7.421  
  12. ======================================  
  13. toc calculates the last loop run time 2.0479  
  14. 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.

[plain] view plain copy
  1. t1=clock;  
  2. 。。。。。。。。。。。  
  3. t2=clock;  
  4. 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

[plain] view plain copy
  1. t0 = cputime  
  2. 。。。。。。。。。。。。。  
  3. 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


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325916548&siteId=291194637