How can I make my code faster?

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 commentaires

Guillaume
Guillaume le 4 Mai 2018
In addition to Stephen's links, some comments explaining what the code do wouldn't go amiss. For example, it looks like you're moving some coordinates randomly in a loop until they're close enough to something. Why? Wouldn't it be faster to simply generate the coordinates within your allowed circle?

Connectez-vous pour commenter.

Réponses (1)

per isakson
per isakson le 4 Mai 2018

0 votes

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
  1. First, create all graphic objects needed ('visible','off')
  2. Then use set to set the attributes of the objects
  3. Avoid calls to drawnow and make the diagram visible at the end.

6 commentaires

Delshad Ayoubi
Delshad Ayoubi le 4 Mai 2018
Modifié(e) : Delshad Ayoubi le 4 Mai 2018
I have to use drawnow since I'm showing how the entropy changes. I just made it into a function file but profile seemingly does not work.
Is there anything else I can do to make it run fast? I have tried removing the for loops as I believe they are the culprit but I don't know how.
Stephen23
Stephen23 le 4 Mai 2018
" I have tried removing the for loops as I believe they are the culprit but I don't know how."
Why do you think that the loops are the problem?
Delshad Ayoubi
Delshad Ayoubi le 4 Mai 2018
Modifié(e) : Delshad Ayoubi le 4 Mai 2018
From my understanding using vectors instead of these for loops is more time efficient. What else would the problem be? Why is it so slow to run and how is it fixed?
per isakson
per isakson le 5 Mai 2018
Modifié(e) : per isakson le 5 Mai 2018
"but profile seemingly does not work" How should I interpret this statement? My preliminary interpretation is that you made a trivial mistake and doesn't care to describe what happened. My conclusion: you don't think running profile would help you.
per isakson
per isakson le 5 Mai 2018
Modifié(e) : per isakson le 5 Mai 2018
"I have to use drawnow" Fine, however item 1 and 2 still apply. You didn't comment on them.
per isakson
per isakson le 5 Mai 2018
Modifié(e) : per isakson le 5 Mai 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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Graphics Performance dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by