Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
how to reduce computation time?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  6 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Peter Schreiber  
View profile  
 More options Apr 16, 2:32 pm
Newsgroups: comp.soft-sys.matlab
From: "Peter Schreiber" <schreiber.pete...@gmail.com>
Date: Thu, 16 Apr 2009 18:32:01 +0000 (UTC)
Local: Thurs, Apr 16 2009 2:32 pm
Subject: how to reduce computation time?
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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
lan  
View profile  
 More options Apr 16, 3:18 pm
Newsgroups: comp.soft-sys.matlab
From: "lan " <amoyjia...@hotmail.com>
Date: Thu, 16 Apr 2009 19:18:00 +0000 (UTC)
Subject: Re: how to reduce computation time?
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.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Matt Fig  
View profile  
 More options Apr 16, 3:33 pm
Newsgroups: comp.soft-sys.matlab
From: "Matt Fig" <spama...@yahoo.com>
Date: Thu, 16 Apr 2009 19:33:01 +0000 (UTC)
Local: Thurs, Apr 16 2009 3:33 pm
Subject: Re: how to reduce computation time?
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)


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Matt Fig  
View profile  
 More options Apr 16, 4:08 pm
Newsgroups: comp.soft-sys.matlab
From: "Matt Fig" <spama...@yahoo.com>
Date: Thu, 16 Apr 2009 20:08:01 +0000 (UTC)
Local: Thurs, Apr 16 2009 4:08 pm
Subject: Re: how to reduce computation time?
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)


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
lan  
View profile  
 More options Apr 16, 4:21 pm
Newsgroups: comp.soft-sys.matlab
From: "lan " <amoyjia...@hotmail.com>
Date: Thu, 16 Apr 2009 20:21:01 +0000 (UTC)
Local: Thurs, Apr 16 2009 4:21 pm
Subject: Re: how to reduce computation time?
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.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Matt  
View profile  
 More options Apr 16, 5:07 pm
Newsgroups: comp.soft-sys.matlab
From: "Matt " <x...@whatever.com>
Date: Thu, 16 Apr 2009 21:07:01 +0000 (UTC)
Local: Thurs, Apr 16 2009 5:07 pm
Subject: Re: how to reduce computation time?

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)


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google