How to best time differences between function implementations

3 次查看(过去 30 天)
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];

回答(1 个)

Jan
Jan 2012-4-19
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 个评论
Daniel Shub
Daniel Shub 2012-4-19
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Performance and Memory 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by