I'm measuring some code in loop
fps = zeros(1, 100);
for i=1:100
t = tic
I = fetch_image_from_source(); % function to get image
fps(i) = 1./ toc(t);
end
plot(fps);
And I get average 50 fps.
Then I'd like to add imshow()
to my code. I understand that imshow
is very slow, but I won't include imshow
inside tic-toc
commands:
fps = zeros(1, 100);
figure;
for i=1:100
t = tic
I = fetch_image_from_source(); % function to get image
fps(i) = 1./ toc(t);
imshow(I); drawnow;
end
plot(fps);
And I get fps about 20%-30% slower. Why does it happen? Because imshow()
is outside tic-toc
Here is a matlab's doc about time in general and how elapsed time was and is currently measured in matlab. We can read that "tic and toc [offers] the highest accuracy and most predictable behavior". I think it is valid statement.
The drop of performance observed here is not due to a bad measure of elapsed time, and not related either to the use of
imshow
ordrawnow
functions. I will argue that it is related to a cache system.The figure below displays the results of four tests, each of them having its own
tic/toc
baseline measure (plotted in blue) for 100 iterations. The green line shows the performance in different conditions:As reported in your question, we can observe a slower frame per second (FPS; I would say 30%) despite
rand
being outside of the tic/toc block. The extra function can be of any type (plot
,surf
,imshow
,sum
), you will always observe a performance drop.In the second subplot, the tic/toc block is repeated twice. The
fps
measurement is therefore executed two times and only the second measure is kept. We see that the performance drop is not there anymore - just like the first tic/toc call prepared the second one (warm-up). I interpret this in term of cache: the instructions and/or data are executed and then kept in a low level memory - the second call is faster.The third subplot used 10,000 tic/toc as an extra function in a single call scenario. You can see the the performance is nearly identical. The whole set of data/instructions in this subplot is only related to tic/toc - again, with a fast cache access.
Finally, the fourth subplot shows two consecutive block of tic/toc calls. We can see that the second one performs better than the first one (a warm-up effect).
The overall pattern shown here is not related to
imshow
, does not depend onJIT
ofaccel
, but depends only on successive calls to a particular function. I interpret this in terms of cache, but I lack some kind of formal evidence.Here are the plots
and the code
This could be due to your processor's multi-threading capability.
The number of computational threads used by MATLAB is based on the value of
maxNumCompThreads
. If you set this to1
, then both cases should theoretically yield the same fps.You can do achieve this as:
Here
N
ought to be1
andLASTN
will give you the previous maximum number of computational threads, which may be useful later in case you want to reset the preference.