Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to reduce computation time?

3 views
Skip to first unread message

Peter Schreiber

unread,
Apr 16, 2009, 2:32:01 PM4/16/09
to
Hi guys,
I wrote the attached code and would like to make it run faster.
Can somebody help me out?
Best Regards,
Peter

for habs=0:.25:1
for pabs=0:.2:1
for beta=0:pi/2:2*pi
for alpha=0:pi/5:2*pi

H=habs*(cos(beta)+i*sin(beta));
p=pabs*(cos(alpha)+i*sin(alpha));
W=abs(H)^2*abs(p)*(2*exp(i*(alpha-beta))+ exp(-i*(alpha-beta)))*exp(i*beta);
plot(real(W+H),imag(W+H),'o','MarkerSize',4)
hold on

end
end
end
end

lan

unread,
Apr 16, 2009, 3:18:00 PM4/16/09
to
Try this, a litter faster:

rea=zeros(1650,1);
ima=zeros(1650,1);
k=0;


for habs=0:.25:1
for pabs=0:.2:1
for beta=0:pi/2:2*pi
for alpha=0:pi/5:2*pi

k=k+1;
H=habs*(cos(beta)+i*sin(beta));
p=pabs*(cos(alpha)+i*sin(alpha));
W=abs(H)^2*abs(p)*(2*exp(i*(alpha-beta))+ exp(-i*(alpha-beta)))*exp(i*beta);
%plot(real(W+H),imag(W+H),'o','MarkerSize',4)
rea(k,1)=real(W+H);
ima(k,1)=imag(W+H);
%hold on

end
end
end
end
scatter(rea,ima);


Note: The idea is using Vectorization for speeding up. You can continue try to speed up more.


"Peter Schreiber" <schreibe...@gmail.com> wrote in message <gs7tj1$gt8$1...@fred.mathworks.com>...

Matt Fig

unread,
Apr 16, 2009, 3:33:01 PM4/16/09
to
Simplify the math, then don't call plot and hold each time through.

cnt = 1; % No pre-allocation for such a small problem.
for habs = 0:.25:1
for pabs = 0:.2:1
for beta = 0:pi/2:2*pi
for alpha = 0:pi/5:2*pi
H = habs*(cos(beta)+i*sin(beta));
p = pabs*(cos(alpha)+i*sin(alpha));
W = abs(H)^2*abs(p)*(2*exp(i*alpha)+ exp(-i*(alpha-2*beta)));
x(cnt) = real(W+H);
y(cnt) = imag(W+H);
cnt = cnt+1;
end
end
end
end
plot(x,y,'o','MarkerSize',4)

Matt Fig

unread,
Apr 16, 2009, 4:08:01 PM4/16/09
to
The math can be even further simplified, which speeds it up some more. The next step would be to start vectorizing to see if that makes a difference.


cnt = 1;


for habs = 0:.25:1
for pabs = 0:.2:1
for beta = 0:pi/2:2*pi
for alpha = 0:pi/5:2*pi

etb = exp(i*beta);
eta = exp(i*alpha);
x(cnt) = habs*(habs*abs(etb)^2*pabs*abs(eta)*(2*eta+ etb^2/eta) + etb);


cnt = cnt+1;
end
end
end
end

plot(real(x),imag(x),'o','MarkerSize',4)

lan

unread,
Apr 16, 2009, 4:21:01 PM4/16/09
to
For reducing computation time, two ways may be useful:
Preallocation and Vectorization.

Besides:
You can also use command profile to find the bottleneck your computation program.

For example:

profile on;
profile clear;

for habs=0:.25:1
for pabs=0:.2:1
for beta=0:pi/2:2*pi
for alpha=0:pi/5:2*pi
H=habs*(cos(beta)+i*sin(beta));
p=pabs*(cos(alpha)+i*sin(alpha));
W=abs(H)^2*abs(p)*(2*exp(i*(alpha-beta))+ exp(-i*(alpha-beta)))*exp(i*beta);
plot(real(W+H),imag(W+H),'o','MarkerSize',4)
hold on

end
end
end
end

profile report;


Through the report, you can find the bottleneck is using 'plot' and 'hold on' many times. Then you may think about how to use Preallocation and Vectorization to reduce the cpu time.

Matt

unread,
Apr 16, 2009, 5:07:01 PM4/16/09
to
"Peter Schreiber" <schreibe...@gmail.com> wrote in message <gs7tj1$gt8$1...@fred.mathworks.com>...


Perhaps as follows. It requires no for-loops and only a single plot command

[habs,pabs,beta,alpha]=ndgrid(0:.25:1,0:.2:1, 0:pi/2:2*pi,0:pi/5:2*pi);
H=habs.*(cos(beta)+i*sin(beta));
p=pabs.*(cos(alpha)+i*sin(alpha));
W=abs(H).^2.*abs(p).*(2*exp(i*(alpha-beta))+ exp(-i*(alpha-beta))).*exp(i*beta);

W=W(:);
H=H(:);

0 new messages