当前位置:网站首页>Timing method of MATLAB program running

Timing method of MATLAB program running

2022-06-21 14:30:00 Modest learning and progress

Be careful : The three methods have different principles , There may be a certain gap in the results !

1、tic and toc Combine ( Use the most )
 

Calculation tic and toc The running time between programs , Its classic format is

 

tic
..........
toc


In other words, the program , Program encounter tic when Matlab Automatically start timing , Run to the toc Automatically calculate this time and the last time tic Time between . This is a bit awkward , Let's give an example to illustrate

 

% by dynamic of 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)
    % Calculate to the last encounter tic Time for , In other words, the time of each cycle 
    disp(['toc Computation first ',num2str(i),' Cycle run time :',num2str(toc)]);
    % Calculate the time of each cycle 
    disp(['etime Computation first ',num2str(i),' Cycle run time :',num2str(etime(clock,t2))]);
    % Calculate the total running time of the program 
    disp(['etime Calculate the running time of the program from the beginning to the present :',num2str(etime(clock,t1))]);
    disp('======================================')
end
% Calculate from this time to tic2 Time for , Because the last time I met tic Is in for Cyclic i=3 when , So we calculate the time of the last cycle 
disp(['toc Calculate the running time of the last cycle ',num2str(toc)])
disp(['etime Total running time of program :',num2str(etime(clock,t1))]);


The operation results are as follows , You can analyze it yourself

 

toc Computation first 1 Cycle run time :2.5628
etime Computation first 1 Cycle run time :2.562
etime Calculate the running time of the program from the beginning to the present :2.562
======================================
toc Computation first 2 Cycle run time :2.8108
etime Computation first 2 Cycle run time :2.813
etime Calculate the running time of the program from the beginning to the present :5.375
======================================
toc Computation first 3 Cycle run time :2.0462
etime Computation first 3 Cycle run time :2.046
etime Calculate the running time of the program from the beginning to the present :7.421
======================================
toc Calculate the running time of the last cycle 2.0479
etime Total running time of program :7.421


2、etime(t1,t2) And on and on clock coordination

To calculate t1,t2 Time difference between , It's done by calling windows The time difference of the system clock is calculated to get the running time , Form of application

 

t1=clock;
...........
t2=clock;
etime(t2,t1)


3、cputime Function to complete

Usage and etime be similar , Only this one uses cpu The dominant frequency of , Different from the previous principle , The format is as follows

 

t0=cputime
.............
t1=cputime-t0


Matlab Official recommendation tic/toc Combine ,When timing the duration of an event, use the tic and toc functions instead of clock or etime.

As for you, you can choose according to your own preferences , But use tic/toc Must pay attention to ,toc The calculation is related to the last run tic Time between , Not the first one tic, Not the second one .....

原网站

版权声明
本文为[Modest learning and progress]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202221424252559.html