why vectorization becomes slower?
显示 更早的评论
I was trying to compute a simple function by 'for' loop but when I vectorized the same function as shown below in 'VECTORISED FORM' instead of improvement in CPU time it becomes slower than 'for' loop, I don't exactly know the reason for this because I am new in vectorization therefore don't even know whether I did vectorization right or wrong
SIMPLE FORM
for i=2:n+1
for j=2:m+1
f1(i,j)= -((+ rho*(((((uyt(i,j) - uyt(i-1,j))/(2*a))+((vxt(i,j) - vxt(i,j-1))/(2*a))))/(2*tao))) + ...
(rho*(((uytold(i,j)-uytold(i-1,j))/(2*a))*((uyt(i,j)-uyt(i-1,j))/(2*a)))) + ...
(rho*(((vyt(i,j)-vyt(i-1,j))/(2*a))*((uxtold(i,j)-uxtold(i,j-1))/(2*b)))) + (rho*(((vytold(i,j)-vytold(i-1,j))/(2*a))*((uxt(i,j)-uxt(i,j-1))/(2*b)))) + ...
(rho*(((vxtold(i,j)-vxtold(i,j-1))/(2*b))*((vxt(i,j)-vxt(i,j-1))/(2*b)))));
end
end
VECTORISED FORM
f1(2:n+1,2:m+1)= -((+ rho.*(((((uyt(2:n+1,2:m+1) - uyt(1:n,2:m+1))./(2.*a))+((vxt(2:n+1,2:m+1) - vxt(2:n+1,1:m))./(2.*a))))./(2.*tao))) + ...
(rho.*(((uytold(2:n+1,2:m+1)-uytold(1:n,2:m+1))./(2.*a)).*((uyt(2:n+1,2:m+1)-uyt(1:n,2:m+1))./(2.*a)))) + ...
(rho.*(((vyt(2:n+1,2:m+1)-vyt(1:n,2:m+1))./(2.*a)).*((uxtold(2:n+1,2:m+1)-uxtold(2:n+1,1:m))./(2.*b)))) + (rho.*(((vytold(2:n+1,2:m+1)-vytold(1:n,2:m+1))./(2.*a)).*((uxt(2:n+1,2:m+1)-uxt(2:n+1,1:m))./(2.*b)))) + ...
(rho.*(((vxtold(2:n+1,2:m+1)-vxtold(2:n+1,1:m))./(2.*b)).*((vxt(2:n+1,2:m+1)-vxt(2:n+1,1:m))./(2.*b)))));
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!