I want to test if is more convenient keep my data in a 2D matrix (in each column I keep differents kinds of information in sorted way) or use multi-dim matrix: m_A(d1*d2,d3) or m_A(d1,d2,d3). If I call a function that have argument m_B(d1,d2), I should send reshape(m_A(:,i3),d1,d3) or m_A(:,:,i3).
My test function is:
function f_ReshapeVsMultiArray nPrueba = 10000; a = 200; m_MultiArray = rand(a,a,nPrueba); m_Array = reshape(m_MultiArray,[],nPrueba); disp('1') tic for iPrueba = 1:nPrueba m_MultiArray(:,:,iPrueba) = f_CalcPorcMat(m_MultiArray(:,:,iPrueba)); end toc disp('2') tic for iPrueba = 1:nPrueba m_Array(:,iPrueba) = reshape(f_CalcPorcMat(reshape(m_Array(:,iPrueba),a,a)),[],1); end toc disp('3') tic nVal = a*a; m_Ind = 1:nVal; for iPrueba = 1:nPrueba m_Array(m_Ind,iPrueba) = reshape(f_CalcPorcMat(reshape(m_Array(m_Ind,iPrueba),a,a)),[],1); end toc disp('4') tic for iPrueba = 1:nPrueba m_ArrayCuadr = m_MultiArray(:,:,iPrueba); m_ArrayCuadr = f_CalcPorcMat(m_ArrayCuadr); m_MultiArray(:,:,iPrueba) = m_ArrayCuadr; end toc disp('5') tic for iPrueba = 1:nPrueba m_ArrayCuadr = reshape(m_Array(:,iPrueba),a,a); m_ArrayCuadr = f_CalcPorcMat(m_ArrayCuadr); m_Array(:,iPrueba) = reshape(m_ArrayCuadr,[],1); end toc disp('6') tic for iPrueba = 1:nPrueba m_ArrayCuadr = reshape(m_Array(:,iPrueba),a,a); m_ArrayCuadr = f_CalcPorcMat(m_ArrayCuadr); m_Array(:,iPrueba) = m_ArrayCuadr(:); end toc disp('7') tic nVal = a*a; m_Ind = 1:nVal; for iPrueba = 1:nPrueba m_ArrayCuadr = reshape(m_Array(m_Ind,iPrueba),a,a); m_ArrayCuadr = f_CalcPorcMat(m_ArrayCuadr); m_Array(m_Ind,iPrueba) = m_ArrayCuadr(:); end toc disp('8') tic for iPrueba = 1:nPrueba m_ArrayCuadr = m_Array(:,iPrueba); m_ArrayCuadr = f_CalcPorcMatReshape(m_ArrayCuadr,a); m_Array(:,iPrueba) = m_ArrayCuadr; end tocendfunction m_Mat = f_CalcPorcMat(m_Mat) m_Mat = m_Mat*m_Mat;endfunction m_Mat = f_CalcPorcMatReshape(m_Mat,a) m_Mat = reshape(m_Mat,a,a); m_Mat = m_Mat*m_Mat; m_Mat = m_Mat(:);end%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
With a = 100:
1: Elapsed time is 2.438622 seconds.
2: Elapsed time is 2.042828 seconds.
3: Elapsed time is 4.672457 seconds.
4: Elapsed time is 2.148157 seconds.
5: Elapsed time is 2.048681 seconds.
6: Elapsed time is 2.011032 seconds.
7: Elapsed time is 3.972383 seconds.
8: Elapsed time is 2.024055 seconds.
With a = 200:
1: Elapsed time is 9.004146 seconds.
2: Elapsed time is 7.572500 seconds.
3: Elapsed time is 15.502879 seconds.
4: Elapsed time is 7.754266 seconds.
5: Elapsed time is 7.577100 seconds.
6: Elapsed time is 7.523504 seconds.
7: Elapsed time is 14.759984 seconds.
8: Elapsed time is 7.642342 seconds.
My questions:
– Why case (1), multi-dim array, is more slow that case (2), reshape and 2D matrix?
– Why case (3), is so more slow that case (2)? The only change is using indexing with matrix vs colon operator :.
– Why case (1) is more slow that case (4), but not with case (2) and (5)? The only differences is using temporary variables and more code lines.
Thanks by any tips o information.
Best Answer