Fast Element-Wise power

2 visualizaciones (últimos 30 días)
Thomas Bauer
Thomas Bauer el 30 de Oct. de 2017
Comentada: Johannes Kalliauer el 1 de Abr. de 2019
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 comentario
Thomas Bauer
Thomas Bauer el 30 de Oct. de 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.

Iniciar sesión para comentar.

Respuesta aceptada

KL
KL el 30 de Oct. de 2017
Editada: KL el 30 de Oct. de 2017
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 comentarios
Thomas Bauer
Thomas Bauer el 30 de Oct. de 2017
Thank you for this example!
Johannes Kalliauer
Johannes Kalliauer el 1 de Abr. de 2019
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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Parallel Computing Toolbox en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by