Filter löschen
Filter löschen

Fastest way to AND or OR together columns of a matrix

2 Ansichten (letzte 30 Tage)
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?

Akzeptierte Antwort

Fangjun Jiang
Fangjun Jiang am 4 Feb. 2019
Bearbeitet: Fangjun Jiang am 4 Feb. 2019
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 Kommentare
Adrian Mariano
Adrian Mariano am 5 Feb. 2019
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 am 5 Feb. 2019
Bearbeitet: Matt J am 5 Feb. 2019
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...

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Fangjun Jiang
Fangjun Jiang am 5 Feb. 2019
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 Kommentare
Fangjun Jiang
Fangjun Jiang am 5 Feb. 2019
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 am 5 Feb. 2019
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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Performance and Memory finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by