Profiler Paradox

2 次查看(过去 30 天)
Mads
Mads 2011-2-16
In an attempt to rotate multiple 3x3 matrices I use two approaches. The first is by running a for loop in which I rotate each job separately. The other approach is where setup a sparse block diagonal matrix and rotate once. In order to find out which is most time-efficient I place tic-toc's around each function and I also use Profiler.
Like this:
tic
function1
toc
tic
function2
toc
The paradox: When using Profiler the function2 is faster, when not using Profiler the function1 is faster. Significantly different (I run several times to get average)
I would expect that using Profiler would yield an overall extended runtime, because it measures everything going on, but I certainly wouldn't expect the two functions switch place in speed.

采纳的回答

Oleg Komarov
Oleg Komarov 2011-2-16
The difference is due to the JIT accelerator that kicks in when using the profiler. Same is if you save the script which calculates the running times and call it from cmd window.
Some useful links:
Oleg

更多回答(3 个)

Walter Roberson
Walter Roberson 2011-2-16
Some releases have inexplicable longer times for a later call when profiling. See a specific case and some test results here

Derek O'Connor
Derek O'Connor 2011-2-16
In certain cases the profiler is worse than useless -- it is misleading. As you will have noticed, the profiler adds a lot of overhead time. This is noise (not wanted) and it can swamp the execution times of the functions you are interested in.
For me, the only reliable numbers that the profiler produces are the statement counts.
See pages 16 and 17 of my notes which discusses an example similar to yours.
Regards,
Derek O'Connor

Matt Tearle
Matt Tearle 2011-3-7
Profiler isn't really the right tool for (absolute) timing a chunk of code - as has been noted, it adds overhead, plus you have to worry about whether JITing is happening or not, etc etc etc. Use the profiler to find bottlenecks/inefficiencies within a piece of code. Use tic and toc (averaged over multiple runs - +10 Cleve points for knowing to do that!) to compare two functions.

类别

Help CenterFile Exchange 中查找有关 Mathematics 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by