How to best time differences between function implementations

조회 수: 2 (최근 30일)
Daniel Shub
Daniel Shub 2012년 4월 19일
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