How to best time differences between function implementations

3 visualizaciones (últimos 30 días)
Daniel Shub
Daniel Shub el 19 de Abr. de 2012
If you have two functions, funA and funB, what is the best way to test if funA takes a different amount o time from funB? Do you use tic/toc, a java timing object, a mex to get the execution times. Once you have the execution times what do you do? As a concrete example consider
funA = @()1:10;
funB = @()[1:10];

Respuestas (1)

Jan
Jan el 19 de Abr. de 2012
There is the stable function FEX: timeit.
For short tests I call something like this from the command line:
tic; for i = 1:1e5, a = funA; clear('a'); end, toc
The clearing avoids surprising JIT effects, most of all when a Mex function is tested. Although the JIT is affected by writing several commands in one line also, I did not see substantial deviations compare to timings measured by creating an M-file with one command per line.
  1 comentario
Daniel Shub
Daniel Shub el 19 de Abr. de 2012
timeit is good, but I think there might be better ways. I don't know if considering only the median is the best way to go. It seems sequntially running timeit for funA and then funB is likely to be more problematic then doing them together. I agree that dealing with the JIT in the timing is important, but of course you still want the JIT to operate on your function.

Iniciar sesión para comentar.

Categorías

Más información sobre Performance and Memory en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by