Say I have many (around 1000) large matrices (about 1000 by 1000) and I want to add them together element-wise. The very naive way is using a temp variable and accumulates in a loop. For example,
summ=0;for ii=1:20 for jj=1:20 summ=summ+ rand(400); endend
After searching on the Internet for some while, someone suggests it's better to do with the help of sum(). For example,
sump=zeros(400,400,400);count=0;for ii=1:20 for j=1:20 count=count+1; sump(:,:,count)=rand(400); endendsumm=sum(sump,3);
However, after I tested two ways, the result is
Elapsed time is 0.780819 seconds.Elapsed time is 1.085279 seconds.
which means the second method is even worse.
So I am just wondering if there any effective way to do addition? Assume that I am working on a computer with very large memory and a GTX 1080 (CUDA might be helpful but I don't know whether it's worthy to do so since communication also takes time.)
Thanks for your time! Any reply will be highly appreciated!.
Best Answer