Why are the times took for consequent tocs not consistent?

7 ビュー (過去 30 日間)
Jeho
Jeho 2011 年 11 月 22 日
We observed a strange behavior that the second toc in "tic;toc;toc;toc" sequence took longer than other tocs being called consequently. What's the reason? A testing function is attached below. See it for yourself and please let me know if you know the reason. Thank you in advance!
%%TICTOCTEST Test the tic-toc stopwatch timer
% Observe a strange behavior that the second toc in "tic;toc;toc;"
% sequence takes longer than other consequent tocs.
%
function tictoctest(k)
%%Memory allocation effect
% to create a null matrix to allocate the memory space first.
% This is to avoid dynamic memory allocation effect in between tocs.
% Dynamic memory allocation delays the second toc even more.
% Comment out the following line and test it again
%tocs = zeros(k,6);
%%For loop
% to repeat "tic;toc;toc;toc;toc;toc;toc" k times and record the time
for i = 1:k
tic;
tocs(i,1) = toc;
tocs(i,2) = toc; % the second toc!
tocs(i,3) = toc;
tocs(i,4) = toc;
tocs(i,5) = toc;
tocs(i,6) = toc;
pause(0.001); % this also affects the results.
end
%%Post processing
% to calculate the differences between consequent tocs.
% This is to minimize the time for calculating differences in the for loop.
elapsed(:,1) = tocs(:,1);
elapsed(:,2) = tocs(:,2) - tocs(:,1);
elapsed(:,3) = tocs(:,3) - tocs(:,2);
elapsed(:,4) = tocs(:,4) - tocs(:,3);
elapsed(:,5) = tocs(:,5) - tocs(:,4);
elapsed(:,6) = tocs(:,6) - tocs(:,5);
%%Plot a boxplot
boxplot(elapsed)
ylim([0 0.00003]) % limit y-axis to see the plot better

採用された回答

Hin Kwan Wong
Hin Kwan Wong 2011 年 11 月 22 日
I would personally use multiple tic tocs instead of tic then multiple tocks. tic tocs are meant to be used as a pair, that's the primay function. Although your usage is supported, it's probably be so by logistics in an obscure way, (e.g. 1st toc release resources and 2nd toc need to dig the last used tic ID from somewhere.) If one uses multiple tocs for less critical timings (several milliseconds) it will be fine. In your case you are measurement is back to front. In addition, the documentation says it is much more accuracte to loop over multiple calculations between the tic and the toc if the calculations take too short of a time.
If you modify your code to include the tic IDs the picture changes, supporting my 'logistics' theory. Now the 1st toc uses slightly more time than the second, than the 3rd, and similar from then on. 2nd still uses more than the 3rd probably as it still have to check through why am I being called a 2nd time and set stage for the 3rd and so on.
for i = 1:k
ID = tic;
tocs(i,1) = toc(ID);
tocs(i,2) = toc(ID); % the second toc!
tocs(i,3) = toc(ID);
tocs(i,4) = toc(ID);
tocs(i,5) = toc(ID);
tocs(i,6) = toc(ID);
pause(0.001); % this also affects the results.
end
If you run paired tic tocs:
for i = 1:k
pause(0.001);
for j=1:6
tic;
tocs(i,j) = toc;
end
end
elapsed = tocs;
Then, only the first toc take longer than the rest, which is forgiveable.
In anycase, you would not use tic toc for measuring very fast timings like this. If you need to do so, use:
tic
for i=1:1000
%some fast code%
end
time = toc;
time = time/1000;
  2 件のコメント
Jeho
Jeho 2011 年 11 月 29 日
Thanks, Hin Kwang Wong! Appreciate your help!
Hin Kwan Wong
Hin Kwan Wong 2011 年 11 月 30 日
Please accept the answer and mark this solved :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangePerformance and Memory についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by