Matlab托克准确性 [英] Matlab tic toc accuracy

查看:97
本文介绍了Matlab托克准确性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在循环测量一些代码

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);

平均速度为50 fps.

And I get average 50 fps.

然后我想将imshow()添加到我的代码中.我知道imshow的运行速度很慢,但是我不会在tic-toc命令中包含imshow:

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);

我将fps降低了约20%-30%.为什么会发生?因为imshow()tic-toc

And I get fps about 20%-30% slower. Why does it happen? Because imshow() is outside tic-toc

推荐答案

这是 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.

此处观察到的性能下降不是由于对经过时间的错误衡量,并且与imshowdrawnow函数的使用无关.我认为这与缓存系统有关.

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 or drawnow functions. I will argue that it is related to a cache system.

下图显示了四个测试的结果,每个测试都有其自己的tic/toc基线测量值(以蓝色绘制),可进行100次迭代.绿线显示了在不同条件下的性能:

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:

(1)    for ii=1:100
         t = tic;                %single tic/toc
         fps(ii,2) = 1./toc(t); 
         rand(1000);             %extra function outside tic/toc
       end

如您的问题中所述,尽管rand不在tic/toc块之外,但我们可以观察到每秒较慢的帧(FPS;我想说是30%).额外功能可以是任何类型(plotsurfimshowsum),您将始终观察到性能下降.

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.

(2)    for ii=1:100
         t = tic;                %first tic/toc
         fps(ii,2) = 1./toc(t); 
         t = tic;                %second tic/toc
         fps(ii,2) = 1./toc(t);
         rand(1000);             %extra function outside tic/toc
       end

在第二个子图中,tic/toc块重复了两次.因此,fps测量执行两次,并且仅保留第二个测量.我们看到性能不再下降-就像第一个tic/toc呼叫准备了第二个tic/toc呼叫(热身)一样.我用缓存来解释:执行指令和/或数据,然后将其保存在低级内存中-第二个调用更快.

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.

(3)    for ii=1:100
         t = tic;                     %first tic/toc
         fps(ii,2) = 1./toc(t);
         for ij = 1:10000             %10,000 extra tic/toc
           tic;
           tmp = toc;
         end
       end

第三个子图在单个调用场景中使用10,000 tic/toc作为额外功能.您可以看到性能几乎相同.该子图中的整个数据/指令集仅与tic/toc有关-同样,具有快速的缓存访问权限.

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.

(4)    for ii=1:100               %first tic/toc block
         t = tic;   
         fps(ii,1) = 1./toc(t);
       end
       for ii=1:100               %second tic/toc block
         t = tic;   
         fps(ii,2) = 1./toc(t);
       end

最后,第四个子图显示了两个连续的tic/toc调用块.我们可以看到第二个比第一个表现更好(预热效果).

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).

此处显示的整体模式与imshow不相关,不取决于accelJIT,而仅取决于对特定函数的连续调用.我用缓存来解释这一点,但是我缺乏某种形式上的证据.

The overall pattern shown here is not related to imshow, does not depend on JIT of accel, 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.

这是情节

和代码

%% EXTRA FUNCTION (single call)
fps = zeros(2, 100);

% first case: 100 tic/toc
for ii=1:100
    t = tic;   
    fps(ii,1) = 1./toc(t);
end

%second case: 100 tic/toc + additional function
for ii=1:100

    t = tic;   
    fps(ii,2) = 1./toc(t);

    % graph or scalar functions (uncomment to test)
    %drawnow;
    %plot(1:10)
    rand(1000);          
    %ones(1000, 1000);
    %sum(1:1000000);
    %diff(1:1000000);
end


h = figure('Color','w','Position',[10 10 600 800]);

subplot(4,1,1);
plot(fps); legend({'tic/toc only','extra function'});
ylabel('FPS');
title('extra function, single call','FontSize',14);
set(gca,'FontSize',14, 'YLim', [0 3.5e5]);

%% EXTRA FUNCTION (double call)
fps = zeros(2, 100);

% first case: 100 tic/toc
for ii=1:100
    t = tic;   
    fps(ii,1) = 1./toc(t);
end

%second case: 100 tic/toc + additional function (except tic/toc)
for ii=1:100

    %first call
    t = tic;   
    fps(ii,2) = 1./toc(t);

    %second call (identical to first)
    t = tic;   
    fps(ii,2) = 1./toc(t);

    rand(1000);
end

subplot(4,1,2);
plot(fps); legend({'tic/toc only','extra function'});
ylabel('FPS');
title('extra function, double call','FontSize',14);
set(gca,'FontSize',14, 'YLim', [0 3.5e5]);


%% EXTRA FUNCTION (double call)
fps = zeros(2, 100);

% first case: 100 tic/toc
for ii=1:100
    t = tic;   
    fps(ii,1) = 1./toc(t);
end

%second case: 100 tic/toc + 10000 tic/toc
for ii=1:100

    t = tic;   
    fps(ii,2) = 1./toc(t);

    for ij = 1:10000
        tic;
        tmp = toc;
    end

end


subplot(4,1,3);
plot(fps); legend({'tic/toc','extra tic/toc'});
ylabel('FPS');
title('Identical function calls','FontSize',14);
set(gca,'FontSize',14, 'YLim', [0 3.5e5]);


%% TIC/TOC call twice
fps = zeros(2, 100);

% first case: 100 tic/toc
for ii=1:100
    t = tic;   
    fps(ii,1) = 1./toc(t);
end

for ii=1:100
    t = tic;   
    fps(ii,2) = 1./toc(t);
end

subplot(4,1,4);
plot(fps); legend({'tic/toc (1)','tic/toc (2)'});
ylabel('FPS');
title('tic/toc twice','FontSize',14);
set(gca,'FontSize',14, 'YLim', [0 3.5e5]);

这篇关于Matlab托克准确性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆