Fastest way to AND or OR together columns of a matrix

조회 수: 1 (최근 30일)
Adrian Mariano
Adrian Mariano 2019년 2월 4일
댓글: Adrian Mariano 2019년 2월 5일
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