How to increase speed of a multi-vector calculation
2 次查看(过去 30 天)
显示 更早的评论
Hello,
I have implemented a loop like this:
for coorC = 1:numel(y1f),
AF_Nunir(:,coorC) = AF_surff( x1f.', y1f.', x1f(coorC), y1f(coorC), (Xr.')*0.015, (Yr.')*0.015 ,K);
end
and:
function z = AF_surff(u , v, u0 , v0 , X, Y , K )
distance = (sqrt((X-u0).^2 + (Y-v0).^2 + 0.4^2) - sqrt((X - u).^2 + (Y - v).^2 + 0.4^2));
AF_out1 = sum(exp(1j*(K.').*(distance(:).')),1);
AF_out2 = sum(reshape(AF_out1,[numel(X),numel(u)]),1);
z = AF_out2;
end
where "K", "Xr", "Yr", "x1f", and "y1f" are vector. I could implement this code using multiple nested "for" loop. But, I'd rather using vector computation in order to increase running speed. Above block must be rUn multiple times in our code, So it is crucial to implement this blcok efficiently. This takes 145s in a "core i9 12900K" CPU. What is your advice to perform this manipulations efficiently? I've heard about parallel computing. Does it effectively play a crucial role for solving this kind of problem?
Any help would be appreciated.
Thank you
0 个评论
回答(1 个)
Rik
2023-12-18
The iterations are not interdependent, so you should be able to use a parfor loop instead.
You could consider performing those conjugations (the .'), since those take up time every iteration.
Another speedup would be to replace distance(:).' by a call to reshape.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!