# How can I make my code faster?

1 view (last 30 days)

Show older comments

clear, clc, close all

%-------------------------------------------------------------%

N = 10^3;

M = 400;

radie = 50;

x=0;

y=0;

z=2;

theta=linspace(0,2*pi);

x_t = zeros(M, N+1);

y_t = zeros(M, N+1);

%-------------------------------------------------------------%

Nbins = 10;

D = 2*radie;

d = D/Nbins;

xc = -D/z + d/z:d:D/z - d/z;

yc = xc;

ctrs = {xc, yc};

%-------------------------------------------------------------%

for n = 1:N

for m = 1:M

distance = radie + 1/N;

while distance > radie

x_t(m, n+1) = x_t(m, n) + (-1).^randi(2);

y_t(m, n+1) = y_t(m, n) + (-1).^randi(2);

distance = hypot(x_t(m, n+1), y_t(m, n+1));

end

end

%

plot(x_t(:, 1:n+1)', y_t(:, 1:n+1).','.', 'MarkerSize', 5);

%

caption = sprintf('Steg %d av %d', n, N);

title(caption, 'FontSize', 25, 'FontWeight', 'bold');

hold on

plot(x+radie*cos(theta),y+radie*sin(theta),'Linewidth',2);

axis equal

hold off

drawnow

%

X = [x_t(:,n) y_t(:,n)];

%-------------------------------------------------------------%

N_i = hist3(X, ctrs);

N_p = M;

N_i = N_i(N_i > 0);

p_i = N_i/N_p;

B=zeros(1,length(p_i));

%

S = 0;

for i = 1:length(p_i)

S = S - p_i(i)*log(p_i(i))

B(:,i) = S;

end

%-------------------------------------------------------------%

end

hist3(X, ctrs);

set(get(gca,'child'),'FaceColor','interp','CDataMode','auto');

grid on

I think I have to somehow remove the for loops but I don't know how. This is a simulation for entropy.

##### 2 Comments

Guillaume
on 4 May 2018

### Answers (1)

per isakson
on 4 May 2018

Firstly, run profile on your code to find the bottlenecks. See profile, Profile execution time for functions. I converted your script to a function and run profile. Plot and drawnow dominated totally. (I have an old graphic card and run Software OpenGL.)

The old trick to make fast graphic

- First, create all graphic objects needed ('visible','off')
- Then use set to set the attributes of the objects
- Avoid calls to drawnow and make the diagram visible at the end.

##### 6 Comments

per isakson
on 5 May 2018

Edited: per isakson
on 5 May 2018

"I have to use drawnow" Fine, however item 1 and 2 still apply. You didn't comment on them.

per isakson
on 5 May 2018

Edited: per isakson
on 5 May 2018

"What else would the problem be?" The problem is that the code creates and destroys zillions of handle graphic objects without any real purpose. Excerpt from plot.m

% If [...] HOLD is off, PLOT resets all axes properties, except Position, to their

% default values, deletes all axes children (line, patch, text, surface, and image

% objects) [...]

Thus, instead of keeping the objects and modify property values the code destroys the objects and creates new ones.

Examples of time thieves:

- the red circle is drawn and erased one thousand times.
- the value of radie, which is constant, controls the axes of the diagram. Despite that the axes, which the user sees, are constant, the limits of the axes are changed on thousand times.
- et cetera.

The poor performance of your code has very little to do with the for-loops.

### See Also

### Categories

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!