Hello all,
I am trying to optimize some code that contains element wise calculations with rather large matrices. I have already shaved off about 50% with things like the bsxfun (which in my case works wonders). But now I am stuck at an operation where a simple line of code value=matrix.^3 takes up most of the runtime of the script. is there any way to perform this element-wise calculation faster?
Best regards

1 commentaire

Thomas Bauer
Thomas Bauer le 30 Oct 2017
Hello, i think i just answered my own question. Appearently using
value=matrix.*matrix.*matrix
runs significantly faster.
BTW i also figured out bsxfun was NOT the reason for my faster running script. It actually was the termination of several operations by precalculating another variable.

Connectez-vous pour commenter.

 Réponse acceptée

KL
KL le 30 Oct 2017
Modifié(e) : KL le 30 Oct 2017

2 votes

a=1:10000000;
b=repmat(3,size(a));
tic
c1=a.^b;
toc
tic
c2=a.^3;
toc
tic
c3=a.*a.*a;
toc
And then the results are,
Elapsed time is 0.118509 seconds.
Elapsed time is 0.129281 seconds.
Elapsed time is 0.006726 seconds.

2 commentaires

Thomas Bauer
Thomas Bauer le 30 Oct 2017
Thank you for this example!
sometimes if you have small integers you can speed it up by useing integers
a=randi([0,6],1,10000000);
b=repmat(3,size(a));
int8a=uint8(a);
tic
c1=a.^b;
toc
tic
c2=a.^3;
toc
tic
c3=a.*a.*a;
toc
tic
c4=int8a.*int8a.*int8a;
toc
And then the results are,
Elapsed time is 0.199174 seconds.
Elapsed time is 0.153210 seconds.
Elapsed time is 0.014326 seconds.
Elapsed time is 0.002569 seconds.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by