Fastest way to AND or OR together columns of a matrix

1 次查看(过去 30 天)
I'm using 2017b and I need to test whether each row of a matrix meets an inequality constraint. This test is done millions of times in my code, so I'm trying to make it as fast as possible. I'm wondering if there is any faster way to do it than this
testdata = rand(1000,26); % setup some fake data
testref = rand(1,26)/20; % setup a fake test reference
result = sum(testdata >= testref,2)==26; % Actual test; can this be faster?

采纳的回答

Fangjun Jiang
Fangjun Jiang 2019-2-4
编辑:Fangjun Jiang 2019-2-4
try
result = all(testdata >= testref,2)
It is quite faster.
Elapsed time is 0.000445 seconds.
Elapsed time is 0.000294 seconds.
Elapsed time is 0.000426 seconds.
Elapsed time is 0.000254 seconds.
Elapsed time is 0.000406 seconds.
Elapsed time is 0.000255 seconds.
  7 个评论
Adrian Mariano
Adrian Mariano 2019-2-5
Like I said, if JIT skipped the loop it would be pretty obvious. But the question remains: what's wrong with testing run time at the command line? How or when will it give misleading results?
Is the conclusion about my original question that using sum() is the fastest method?
Matt J
Matt J 2019-2-5
编辑:Matt J 2019-2-5
But the question remains: what's wrong with testing run time at the command line?
Whatever optimizations Matlab does, it only does them in function files, so command line execution is never the reliable way to see the best that Matlab can do.
Is the conclusion about my original question that using sum() is the fastest method?
We certainly can't conclude that in general. As you can see from my timing tests above, I found all() to be faster, which makes sense because it involves fewer function calls. Possibly, though, you have a very strange computer...

请先登录,再进行评论。

更多回答(1 个)

Fangjun Jiang
Fangjun Jiang 2019-2-5
It looks like depending on the size of the test data. If the data has 1e6 rows or less, sum() is faster. If the data has 1e7 rows or more, all() is faster.
rng(1);
testdata = rand(1e6,26); % setup some fake data
testref = rand(1,26)/20; % setup a fake test reference
cmp=(testdata >= testref);
tic;
result = sum(cmp,2)==26;
t_sum=toc;
tic
result = all(cmp,2);
t_all=toc;
if t_sum<t_all
fprintf('sum() is faster\n\n');
else
fprintf('all() is faster\n\n');
end
  7 个评论
Fangjun Jiang
Fangjun Jiang 2019-2-5
I did try cputime() but didn't find difference when running it once (both are zero). You could try it if you really want to find it out. Again, I thought the choice for using all() was clear but tic/toc indicated otherwise. I guess there are still some explanation needed.
Adrian Mariano
Adrian Mariano 2019-2-5
I tried cputime running on a case with 10^8 rows, and I get a big difference, 3 s for all and 12.4s for sum.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Performance and Memory 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by