Is there any other function faster than sum(A,2) to get the sum of all rows?

조회 수: 18 (최근 30일)
Nadatimuj
Nadatimuj 2022년 6월 26일
댓글: Walter Roberson 2022년 6월 27일
a=zeros(6750);
h=zeros(6750,1);
tic;k=h-sum(a,2); toc
Elapsed time is 0.041132 seconds.
Is there any faster way than to use sum(A,2) to get the sums of the rows of a matrix? sum(A,2) seems to be slow for large matrices.
  댓글 수: 4
Image Analyst
Image Analyst 2022년 6월 27일
The Parallel Processing Toolbox is an add-on toolbox. You can ask for a free trial of it and see if the Parallel Processing Toolbox helps. I know some functions recognize if you have that toolbox and start parallel processes going on multiple CPU cores on your computer automatically.
Walter Roberson
Walter Roberson 2022년 6월 27일
For sufficiently large matrices, addition is automatically handled by high performance parallel libraries. Using parfor or parfeval() will not improve performance compared to the automatic multi-core work that is done.

댓글을 달려면 로그인하십시오.

답변 (2개)

Walter Roberson
Walter Roberson 2022년 6월 26일
Switching to columns can improve performance as it allows better use of hardware cache.
  댓글 수: 2
Nadatimuj
Nadatimuj 2022년 6월 26일
Thanks for the suggestion. Did you mean this? Doesn't look like to save too much time...
a=zeros(6750);
h=zeros(1,6750);
tic;k=h-sum(a,1); toc
Elapsed time is 0.033019 seconds.
Walter Roberson
Walter Roberson 2022년 6월 27일
When I test on my system, the sum(a,1) version is 5 to 6 times faster than the sum(a,2) version

댓글을 달려면 로그인하십시오.


MJFcoNaN
MJFcoNaN 2022년 6월 26일
In general, the native functions are optimized sufficiently.
In certain cases, there may be some better way but in other parts of code. For example,
a=sparse(zeros(6750));
h=sparse(zeros(6750,1));
tic;k=h-sum(a,2); toc
Elapsed time is 0.003234 seconds.
  댓글 수: 1
Nadatimuj
Nadatimuj 2022년 6월 26일
Thanks, yes that's a good idea. However, originally I didn't use the sparse notation, I will check if using sparse will impact other parts of the code. Otherwise sparse conversion just here can take some time.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Performance and Memory에 대해 자세히 알아보기

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by