Is it possible to change the 'ClockPrecision' setting of MATLAB's Profiler?

The default ClockPrecision value is apparently 0.001 seconds. If a function or line takes less time than that, it registers as 0. I'm trying to assess the efficiency of a few different algorithms to compare them, but none of them take long enough to round up to 1 millisecond. I could not find any way to change this, since it isn't one of the options like 'timer' and 'history' according to the documentation page:
However, I did find this page:
which does not describe how to change the setting, but shows a different value (100 nanoseconds) than what I have (1 millisecond). I'm hoping that means it can be changed, but I can't figure out how.
Thanks in advance for any help.

 Réponse acceptée

John D'Errico
John D'Errico le 25 Juin 2015
Modifié(e) : John D'Errico le 25 Juin 2015
Use timeit instead to test and compare specific code blocks. It does various things to make that test far more accurate than the profiler. Steve Eddins had at written at posted it on the file exchange, but it is now part of MATLAB, so no download is needed at all.
While the profiler is very nice and very useful to identify problems in your code, you need a tool like timeit to truly optimize performance once you know where the problem lies.

5 commentaires

Great suggestion, thank you! I looked at the help documentation for timeit and found tic and toc too, which also seem very convenient. Is timeit more accurate than tic and toc, or is there a reason to prefer one over another?
tic and toc record how long one particular execution took. timeit runs the code many times to reduce the uncertainty of measurement and effects such as the time it can take to parse code the first time it is encountered.
IMHO, do NOT use tic and toc for any kind of accurate assessment.
The problem is that there are many reasons why a time assessor may mis-estimate the time taken.
1. On modern CPUs, there are many things happening at once. Stuff happens in the background, often at random. Be careful not to use your web browser or check your mail in the middle of a long time test. You need to make multiple calls to the code to try to average the noise out. Timeit is intelligent, running code that is fast MANY times inside a loop to get the most benefit from variance reduction in a large sample.
2. The first call to a function will always be slower than successive calls, because MATLAB must parse the code and put it into cache. In fact the second call is often a bit slower too. So I often tend to do several time tests to make sure I am seeing a stable prediction. I recall that timeit actually runs your code several times up front (tossing out the time for those initial calls) just for this reason.
3. A code block timed directly at the command line may be different in terms of the time required for code inside a function. This is because the parser may be able to optimize code when it does the parsing, whereas code typed in on the command line see no such benefit. Since code passed into timeit is run as a function inside the function, I believe it will yield a better estimate of how you will be using it because it takes full advantage of any acceleration that will happen.
So given that timeit is available, I would always use it preferentially. I do use tic and toc occasionally, but only for places where I just want to report a rough estimate of the time taken.
Thanks very much for the explanations. I tried them both but they always disagreed. It's good to know a little about what's going on "under the hood."
Later versions of MATLAB can do a fair bit more optimization of expressions at the command line, apparently. For many years, commands at the command line and code in scripts were unoptimized.

Connectez-vous pour commenter.

Plus de réponses (1)

The ClockPrecision is based on the timer used, change the profile timer source for a different precision. If you have access to R2015b the processor timer gives the highest precision.
profile -Timer real %

Catégories

En savoir plus sur Performance and Memory dans Centre d'aide et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by