Toc returns loop time instead of total script time

Hi
I have a script with a large loop nested inside another loop, and i would like to time the progress of the loop, using the tic/toc commands.
The code is like this:
tic
--some code and small loops--
for i=1:lengthi
for j=1:lengthj
clc
disp(['Iteration: ' num2str((i-1)*lengthj+j) ' of ' num2str(lengthi*lengthj)]);
toc
--some code--
end
end
toc
however, when I run the code, toc seems to display the time since last toc, instead of the time since tic. The problem is not exclusive to this piece of code, I have noticed it before with other scripts.
Any thoughts or experience with this?
-Nicolas

3 件のコメント

Nicolas Schnedler-Meyer
Nicolas Schnedler-Meyer 2015 年 4 月 28 日
I've tried replacing tic with tstart=tic, and then toc(tstart) later, and that works as intended.
but i still don't know why it doesn't work otherwise.
Greig
Greig 2015 年 4 月 28 日
What version are you using?
I encounter something similar in 2014a with a particular piece of code, but I could never replicate in another code. I spent ages looking for other tics or clears, but was pretty sure there were none.
Nicolas Schnedler-Meyer
Nicolas Schnedler-Meyer 2015 年 4 月 29 日
It happens both in version 2014b and 2015a.

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

回答 (4 件)

pfb
pfb 2015 年 4 月 27 日
編集済み: pfb 2015 年 4 月 27 日

0 投票

Hi,
I've tried this
tic;
for k=1:10
pause(2);
toc;
end
pause(2);
toc;
on both R2012b and R2014b and the output is what I expect: after each toc 2 seconds are passed
Elapsed time is 2.000970 seconds.
Elapsed time is 4.002267 seconds.
[...]
Elapsed time is 22.011765 seconds.

4 件のコメント

Nicolas Schnedler-Meyer
Nicolas Schnedler-Meyer 2015 年 4 月 28 日
Well, that works for me too, but not in the aforementioned script
pfb
pfb 2015 年 4 月 28 日
編集済み: pfb 2015 年 4 月 28 日
And, just for the sake of stating the obvious, there is no "tic" or similar in "-- some code --", right?
Have you tried different measures of time? Like using "clock" and "etime"?
Like
t0=clock;
% --- some code
et = etime(t0,clock);
fprintf('Elapsed time is %1.3f seconds\n',et);
Nicolas Schnedler-Meyer
Nicolas Schnedler-Meyer 2015 年 4 月 28 日
there's no tic in "-- some code --", no.
Haven't tried other measures, I'm just mystified by toc not working properly..
pfb
pfb 2015 年 4 月 28 日
I can understand that... It's puzzling.
I'd just try and look whether etime and clock are similarly affected by this thing, or else if this is a problem with tic and toc alone...

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

Ilham Hardy
Ilham Hardy 2015 年 4 月 28 日

0 投票

Apart from tic and toc, there is also profile command to check the bottleneck of the script/function.
Jan
Jan 2015 年 4 月 28 日

0 投票

Do you have a 'clear all' anywhere in your code?
Philip Borghesani
Philip Borghesani 2015 年 4 月 29 日
編集済み: Philip Borghesani 2015 年 4 月 29 日

0 投票

Clear all and clc will not clear tic time.
There must be another call to tic. For this reason I recommend always using the t0=tic; t=toc(t0) form of these functions.
To find other calls to tic try dbstop in tic or write your own version of tic that checks for other uses.
A foolproof way to find the call to tic with no output is to fix your outer loop to call t0=tic and place this tic.m in your current directory:
function t0=tic
if nargout==0
dbstack
keyboard
end
t0=builtin('tic');
end

カテゴリ

ヘルプ センター および File ExchangePerformance and Memory についてさらに検索

タグ

質問済み:

2015 年 4 月 27 日

編集済み:

2015 年 4 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by