Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Improving performance

Subject: Improving performance

From: Themis

Date: 4 Mar, 2009 02:31:01

Message: 1 of 3

Hi everyone, i was wondering if someone can help me by finding a way to improve performance on the following function. I shall try to explain as best as I can the equation im trying to implement and then provide my current code.

I am trying to calculate the following sum
1/sqrt(N) * SUM(n=1 to n=N) [ cos( (n^k)*(m/M) ]
m takes value from 1 to M
M and N are the inputs

I need to calculate the above sum for all values of m

My code is as follows:

function [e] = new5(M,N)

tic;

k=3;
n=(1:1:N);
d=zeros(1,M-1);
b=zeros(1,M-1);

for m=1:1:M-1;
    d=m./M;
    b=cos((n.^k)*d);
    e(m)=(sum(b)/sqrt(N));
end

hold on
%plot histogram of data
hist(e,100);figure(gcf);
hold off
toc;

Subject: Improving performance

From: Matt Fig

Date: 4 Mar, 2009 03:25:03

Message: 2 of 3

"Themis " <thkountouris@hotmail.com> wrote in message <gokp55$ol8$1@fred.mathworks.com>...
Well this improves performance for M = 800 N = 800. The cos calls take the most time by far. For the original my elapsed time is .504 seconds and this gives .326 seconds.




k=3;
n = (1:1:N).^k;
d = (1:M-1)/M;
e = zeros(1,M-1);
sqrtN = sqrt(N);

for m=1:1:M-1;
    e(m) = sum(cos(n*d(m)))/sqrtN;
end

Subject: Improving performance

From: Jos

Date: 4 Mar, 2009 10:35:02

Message: 3 of 3

"Themis " <thkountouris@hotmail.com> wrote in message <gokp55$ol8$1@fred.mathworks.com>...
> Hi everyone, i was wondering if someone can help me by finding a way to improve performance on the following function. I shall try to explain as best as I can the equation im trying to implement and then provide my current code.
>
> I am trying to calculate the following sum
> 1/sqrt(N) * SUM(n=1 to n=N) [ cos( (n^k)*(m/M) ]
> m takes value from 1 to M
> M and N are the inputs
>
> I need to calculate the above sum for all values of m
>
> My code is as follows:
>
> function [e] = new5(M,N)
>
> tic;
>
> k=3;
> n=(1:1:N);
> d=zeros(1,M-1);
> b=zeros(1,M-1);
>
> for m=1:1:M-1;
> d=m./M;
> b=cos((n.^k)*d);
> e(m)=(sum(b)/sqrt(N));
> end
>
> hold on
> %plot histogram of data
> hist(e,100);figure(gcf);
> hold off
> toc;

Two very basic tips:
1) Pre-allocate the result "e"
2) get evertthing that does mot depend on the loop outside


k=3;
n=(1:1:N);
% b & d are be overwritten !!
% d=zeros(1,M-1);
% b=zeros(1,M-1);

e = zeros(1,M-1) ; % 1) pre-allocation

% 2) put everything fixed outside loop
d = (1:M-1) ./ M ;
npk = (n .^ k)
sqrtN = sqrt(N) ;

for ix = 1: numel(d),
     b=cos(npk * d(ix));
     e(ix)=(sum(b)/sqrtN);
end

This should speed up things considerably.

hth
Jos

Tags for this Thread

No tags are associated with this thread.

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.

Contact us