How can I make my code faster?
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Guillaume
2018-5-4
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?
回答(1 个)
per isakson
2018-5-4
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 个评论
per isakson
2018-5-5
编辑:per isakson
2018-5-5
"I have to use drawnow" Fine, however item 1 and 2 still apply. You didn't comment on them.
per isakson
2018-5-5
编辑:per isakson
2018-5-5
"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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Performance 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!