Borrar filtros
Borrar filtros

Fastest way to AND or OR together columns of a matrix

2 visualizaciones (últimos 30 días)
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?

Respuesta aceptada

Fangjun Jiang
Fangjun Jiang el 4 de Feb. de 2019
Editada: Fangjun Jiang el 4 de Feb. de 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 comentarios
Adrian Mariano
Adrian Mariano el 5 de Feb. de 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 el 5 de Feb. de 2019
Editada: Matt J el 5 de Feb. de 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...

Iniciar sesión para comentar.

Más respuestas (1)

Fangjun Jiang
Fangjun Jiang el 5 de Feb. de 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 comentarios
Fangjun Jiang
Fangjun Jiang el 5 de Feb. de 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 el 5 de Feb. de 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.

Iniciar sesión para comentar.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by