Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Performance 索引创建的性能_Performance_Matlab - Fatal编程技术网

Performance 索引创建的性能

Performance 索引创建的性能,performance,matlab,Performance,Matlab,在尝试选择推荐哪种索引方法时,我尝试测量性能。然而,这些测量结果让我很困惑。我以不同的顺序运行了多次,但测量结果保持一致。 以下是我衡量绩效的方式: for N = [10000 15000 100000 150000] x = round(rand(N,1)*5)-2; idx1 = x~=0; idx2 = abs(x)>0; tic for t = 1:5000 idx1 = x~=0; end toc

在尝试选择推荐哪种索引方法时,我尝试测量性能。然而,这些测量结果让我很困惑。我以不同的顺序运行了多次,但测量结果保持一致。 以下是我衡量绩效的方式:

for N = [10000 15000 100000 150000]
    x =  round(rand(N,1)*5)-2;
    idx1 = x~=0;
    idx2 = abs(x)>0;

    tic
    for t = 1:5000
        idx1 = x~=0;
    end
    toc

    tic
    for t = 1:5000
        idx2 = abs(x)>0;
    end
    toc
end
这就是结果:

Elapsed time is 0.203504 seconds.
Elapsed time is 0.230439 seconds.

Elapsed time is 0.319840 seconds.
Elapsed time is 0.352562 seconds.

Elapsed time is 2.118108 seconds. % This is the strange part
Elapsed time is 0.434818 seconds.

Elapsed time is 0.508882 seconds.
Elapsed time is 0.550144 seconds.
我检查了100000左右的值,这种情况也会发生,即使是50000,也会发生奇怪的测量


所以我的问题是:在一定范围内,有没有其他人经历过这种情况,是什么原因造成的?(这是一个bug吗?

我认为这与JIT有关(下面的结果使用的是2011b)。根据系统、Matlab版本、变量大小以及循环中的具体内容,使用JIT并不总是更快。这与“预热”效应有关,有时如果在一个会话中多次运行m文件,则在第一次运行后会更快,因为加速器只需编译一次代码的某些部分

JIT开启(功能加速开启)

JIT关闭(功能加速关闭)

ETA,有点有趣,看看如果使用整数而不是双精度:

JIT开启,代码相同,但使用int8转换了x

Elapsed time is 0.202201 seconds.
Elapsed time is 0.192103 seconds.

Elapsed time is 0.294974 seconds.
Elapsed time is 0.296191 seconds.

Elapsed time is 2.001245 seconds.
Elapsed time is 2.038713 seconds.

Elapsed time is 0.870500 seconds.
Elapsed time is 0.898301 seconds.
Elapsed time is 0.198611 seconds.
Elapsed time is 0.187589 seconds.

Elapsed time is 0.282775 seconds.
Elapsed time is 0.282938 seconds.

Elapsed time is 1.837561 seconds.
Elapsed time is 1.846766 seconds.

Elapsed time is 2.746034 seconds.
Elapsed time is 2.760067 seconds.
使用int8关闭JIT

Elapsed time is 0.202201 seconds.
Elapsed time is 0.192103 seconds.

Elapsed time is 0.294974 seconds.
Elapsed time is 0.296191 seconds.

Elapsed time is 2.001245 seconds.
Elapsed time is 2.038713 seconds.

Elapsed time is 0.870500 seconds.
Elapsed time is 0.898301 seconds.
Elapsed time is 0.198611 seconds.
Elapsed time is 0.187589 seconds.

Elapsed time is 0.282775 seconds.
Elapsed time is 0.282938 seconds.

Elapsed time is 1.837561 seconds.
Elapsed time is 1.846766 seconds.

Elapsed time is 2.746034 seconds.
Elapsed time is 2.760067 seconds.

这可能是由于matlab使用了一些自动优化的基本线性代数子程序

与您的配置一样,我的配置(OSX 10.8.4,R2012a,带有默认设置)计算x(10e5个元素)的
idx1=x~=0
比计算x(11e5个元素)花费的时间更长。请参见图的左面板,其中测量了不同矢量大小(x轴)的处理时间(y轴)。当N>103000时,您将看到较低的处理时间。在这个面板中,我还显示了计算过程中活动的核心数。您将看到单核配置没有下降。这意味着当1个内核处于活动状态时,matlab不会优化
~=
的执行(不可能并行化)。当满足两个条件时,Matlab启用一些优化例程:多核和足够大小的向量

功能('accel','on'/off')
设置为off()时,右面板显示结果。在这里,只有一个核是活动的(1核和4核是相同的),因此不可能进行优化

最后,我用于激活/停用内核的函数是
maxNumCompThreads
。根据,maxNumCompThreads控制JIT和。由于
功能('JIT','on'/'off')
在性能中没有起作用,BLAS是剩下的最后一个选项

我将把最后一句话留给Loren:“这里的主要信息是,您根本不需要使用这个函数[maxNumCompThreads]。为什么?因为我们想让MATLAB尽可能为您做好最好的工作。”


好吧,我肯定会假设
abs(x)>0
会更慢,因为它确实在做2个操作,但是100000的N次试验并没有遵循这一点。奇怪。然而,我几乎总是使用
x~=0
,因为它只执行一个操作。还要注意的是,这两者之间的差异对我来说并没有你那么大。第三次试验的间隔仅为0.4秒,而不是1.5秒。我只认为在10万次试验的背景下会出现一些奇怪的内存分配。我看到了同样的情况,但没有看到那么明显(R2012b,OS X 10.8.4)。我不会“假设”abs(x)>0执行两个操作。JIT编译后,可以在比较中忽略符号位。实际上,
x~=0
的情况更为复杂(相当于
x>0 | xin有趣的是,关闭JIT实际上会加快速度。但是,如果预热是个问题,这并不能解释为什么100000比150000慢。请注意,如果更改顺序
N=[10000 15000 150000 100000]
我认为预热在命令行上不起作用。我基本上猜测使用JIT会有一些开销,这可能是可变大小的函数,也有一些好处,这也是可变大小的函数。最终好处大于开销,但直到x>150000左右(在这种情况下)。它实际上可能不是来自JIT-请看我的答案。我猜理想的优化会在它带来更好的结果时立即开始。因此,如果我理解正确的话:对于这个操作(在我们的计算机上),优化只是开始得太晚了?是的-你已经找到了“最适合你的工作”之一但是matlab的开发人员肯定意识到了这一点,并且故意这么做。
accel = {'on';'off'};
figure('Color','w');
N = 100000:1000:105000;

for ind_accel = 2:-1:1
    eval(['feature(''accel'',''' accel{ind_accel} ''')']);
    tElapsed = zeros(4,length(N));
    for ind_core = 1:4
        maxNumCompThreads(ind_core);
        n_core = maxNumCompThreads;
        for ii = 1:length(N)
            fprintf('core asked: %d(true:%d) - N:%d\n',ind_core,n_core, ii);
            x =  round(rand(N(ii),1)*5)-2;
            idx1 = x~=0;
            tStart = tic;
            for t = 1:5000
                idx1 = x~=0;
            end
            tElapsed(ind_core,ii) = toc(tStart);
        end
    end
    h2 = subplot(1,2,ind_accel);
    plot(N, tElapsed,'-o','MarkerSize',10);
    legend({('1':'4')'});
    xlabel('Vector size','FontSize',14);
    ylabel('Processing time','FontSize',14);
    set(gca,'FontSize',14,'YLim',[0.2 0.7]);
    title(['accel ' accel{ind_accel}]);
end