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);
tocs(i,3) = toc(ID);
tocs(i,4) = toc(ID);
tocs(i,5) = toc(ID);
tocs(i,6) = toc(ID);
pause(0.001);
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
end
time = toc;
time = time/1000;
Best Answer