Thread Subject: how to reduce computation time?

Subject: how to reduce computation time?

From: Peter Schreiber

Date: 16 Apr, 2009 18:32:01

Message: 1 of 6

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

Subject: how to reduce computation time?

From: lan

Date: 16 Apr, 2009 19:18:00

Message: 2 of 6

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" <schreiber.peter15@gmail.com> wrote in message <gs7tj1$gt8$1@fred.mathworks.com>...
> 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

Subject: how to reduce computation time?

From: Matt Fig

Date: 16 Apr, 2009 19:33:01

Message: 3 of 6

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)

Subject: how to reduce computation time?

From: Matt Fig

Date: 16 Apr, 2009 20:08:01

Message: 4 of 6

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)

Subject: how to reduce computation time?

From: lan

Date: 16 Apr, 2009 20:21:01

Message: 5 of 6

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.

Subject: how to reduce computation time?

From: Matt

Date: 16 Apr, 2009 21:07:01

Message: 6 of 6

"Peter Schreiber" <schreiber.peter15@gmail.com> wrote in message <gs7tj1$gt8$1@fred.mathworks.com>...
> 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


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(:);
plot(real(W+H),imag(W+H),'o','MarkerSize',4)

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us at files@mathworks.com