Speed and Multiple Vectors
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Christopher
el 26 de Feb. de 2014
Editada: per isakson
el 26 de Feb. de 2014
Is it faster to operate on multiple individual vectors or to put many vectors into a single matrix and simply reference the columns of the resulting matrix?
0 comentarios
Respuesta aceptada
per isakson
el 26 de Feb. de 2014
Editada: per isakson
el 26 de Feb. de 2014
This test probably has little meaning. (At least, Matlab is not smart enough to see that I take max of the same vector thousand times.) I guess, the difference is because the vector remains in a cache closer to the cpu. If speed is important, you might want to make a more realistic test.
N = 1000;
M = rand(1e5,N);
v = M(:,1);
tic
for jj = 1 : N
m = max(M(:,jj));
end
toc
tic
for jj = 1 : N
m = max(v);
end
toc
returns
Elapsed time is 0.465941 seconds.
Elapsed time is 0.036675 seconds.
.
One column only:
N = 1000;
M = rand(1e5,N);
v = M(:,1);
JJ = floor(N/2);
tic
for jj = 1 : N
m = max( M( :, JJ ) ); % <<<<<<<<<<<
end
toc
tic
for jj = 1 : N
m = max(v);
end
toc
returns
Elapsed time is 0.340061 seconds.
Elapsed time is 0.046989 seconds.
0 comentarios
Más respuestas (1)
Star Strider
el 26 de Feb. de 2014
I suggest combining them into a single matrix and referencing the columns of the resulting matrix. It’s a lot easier to code and store those results. This works for vectors of equal lengths.
If you encounter the problem of the vectors having different lengths, it°s easy to use a cell array to store them and have them behave essentially as a matrix (except for not being able to use that matrix in matrix computations). It is easy to convert them back to numerical vectors for computation when you need to.
0 comentarios
Ver también
Categorías
Más información sobre Performance and Memory en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!