Is tic/toc really reliable?

Hello,
I am comparing 3 ways of calculating a simple stuff (cutting b in half until b<a), and tic/toc gives me VERY improbable results.
Here is my entire code (R2014b):
_____________________________________________
function [W,L,LC]=while_vs_log(a,b)
% test which one of a log2 and a while loop is the fastest to reduce b until it unders a.
if (b<a) fprintf('b must be >= a\n'); return; end
if (a<=0) fprintf('a must be >0\n'); return; end
%%log compact
ticLC = tic;
LC = b/2^ceil(log2(b/a));
tocLC = toc(ticLC);
%%log
ticL = tic;
n = ceil(log2(b/a));
L = b/2^n;
tocL = toc(ticL);
%%while
ticW = tic;
while (b>a) b=b/2; end
W=b;
tocW = toc(ticW);
%%compare
fprintf('while loop : %e µs\n', 1e6*tocW);
fprintf('log2 : %e µs\n', 1e6*tocL);
fprintf('log2 compact: %e µs\n\n',1e6*tocLC);
end
_____________________________________________
I called the function several times, changing absolutely nothing between each call.
Have a look at the output: sometimes, the while loop and the log2 computation appear to take the EXACT same time, which is absolutely unlikely.
My question is: how can it be? And how can I trust tic/toc if it sometimes fail? Or do I use it the wrong way?

4 commentaires

William Frane
William Frane le 5 Déc 2014
When using tic/toc to time relatively simple code, it's typical to run the code N times (for large values of N) between tic and toc and then calculate the average time. See the section "Time Smaller Sections of Code" on this page.
C.J. Harris
C.J. Harris le 5 Déc 2014
Also depends on your operating system. Unless you are using a real-time operating system you are going to get jitter which is much larger than the microsecond time-scales you are trying to measure.
B. C.
B. C. le 5 Déc 2014
@William : Yes, I will average several run times later, this was just a quick start.
@C.J. : Ok, I had no idea about jitter before.
Thanks to both of you.
Andrew Newell
Andrew Newell le 5 Déc 2014

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Language Fundamentals dans Centre d'aide et File Exchange

Produits

Question posée :

le 5 Déc 2014

Commenté :

le 5 Déc 2014

Community Treasure Hunt

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

Start Hunting!

Translated by