How to best time differences between function implementations

3 views (last 30 days)
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];

Answers (1)

Jan
Jan on 19 Apr 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 Comment
Daniel Shub
Daniel Shub on 19 Apr 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.

Sign in to comment.

Categories

Find more on Performance and Memory in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!