我想知道如何在给定的代码片段中处理瓶颈。
%% Points is an Nx3 matrix having the coordinates of N points where N ~ 10^6
Z = points(:,3)
listZ = (Z >= a & Z < b); % Bottleneck
np = sum(listZ); % For later usage
slice = points(listZ,:);目前对于N ~ 10^6,np ~ 1000和number of calls to this part of code = 1000,瓶颈语句总共花费了大约10秒,与我的其余代码相比,这是一个很大的时间块。

根据@EitanT的请求,仅针对索引语句的示例代码的更多屏幕截图


发布于 2013-06-21 00:25:02
如果一侧的相等性不重要,你可以将其重新表示为单边比较,它会快一个数量级:
Z = rand(1e6,3);
a=0.5; b=0.6;
c=(a+b)/2;
d=abs(a-b)/2;
tic
for k=1:100,
listZ1 = (Z >= a & Z < b); % Bottleneck
end
toc
tic
for k=1:100,
listZ2 = (abs(Z-c)<d);
end
toc
isequal(listZ1, listZ2)返回
Elapsed time is 5.567460 seconds.
Elapsed time is 0.625646 seconds.
ans =
1发布于 2013-06-21 00:11:51
假设最坏的情况:
& is not shortis
您将在大约10秒内完成2*1e6*1e3 = 2e9比较。这就是每秒约2亿次比较(约200MFLOPS)。
考虑到你可以做一些1.7 GFLops on a single core,这看起来确实很低。
您运行的是Windows 7吗?如果有,你检查过你的电源设置了吗?您使用的是移动处理器,所以我希望在默认情况下,会有一些低功耗的方案生效。这允许窗口按比例降低处理速度,so...check。
除此之外……我真的一无所知。
发布于 2013-06-20 23:58:35
试着这样做:
for i = 1:1000
x = (a >= 0.5);
x = (x < 0.6);
end我发现它比:
for i = 1:1000
x = (a >= 0.5 & a < 0.6);
end大约4秒:
Elapsed time is 0.985001 seconds. (first one)
Elapsed time is 4.888243 seconds. (second one)我认为慢的原因是element wise &操作。
https://stackoverflow.com/questions/17217430
复制相似问题