Computational Science Asked by Sam Christensen on September 2, 2021
Half the run time of my code right now is evaluating a big function over many, many points, it takes maybe about 20 seconds per evaluation
The function consists of a bunch of simple operations that look like
Ux = ((1./72).*(x.^2+y.^2+z.^2).^(-5./2).*((-5).*x.*(4.*x.^4+3.*y.^2.*( ...
y.^2+z.^2)+x.^2.*(7.*y.^2+z.^2)).*gx^2+(-10).*y.*(3.*x.^4+5.* ...
x.^2.*y.^2+2.*y.^2.*(y.^2+z.^2)).*gx.*gy+5.*x.*(x.^4+3.*y.^2.* ...
z.^2+x.^2.*(y.^2+z.^2)).*gy^2));
is there any way to speed this up? Additionally it spends like 10% of this function evaluation concatenating these large matrices as
U =[Ux;Uy;Uz];
Are there any tricks to speeding up these sorts of evaluations?
I see that you are doing some redundant computation, for example y.^2+z.^2
is computed 4 times, with y.^2
and z.^2
are computed 9-10 times. You can define a set of variables y2=y.^2
, z2=y.^2
, y2pz2=y2+z2;
and push some of the computation cost to memory -given that you have enough memory. That would save you a good amount of time.
MATLAB is a column-major language. Looking at your code, Ux
, Uy
and Uz
seem to be row vectors and you are putting them on top of each other U =[Ux;Uy;Uz];
. That will be slow. If possible, defining U =[Ux',Uy',Uz'];
should reduce the time spent concatenating.
Correct answer by Abdullah Ali Sivas on September 2, 2021
Have you tried arrayfun? If the arrays are really large (how large are we speaking, by the way?), it might be possible that allocating all those temporaries has a cost that arrayfun would save.
Also, instead of concatenating, you could preallocate U and fill it one block at a time.
Answered by Federico Poloni on September 2, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP