I have a vectorized function which calculates distance to a large set of points. To improve performance I am limiting the number of the points by selecting only the necessary ones. So instead of distanceToPoint(points.x)
I am using distanceToPoint(points.x(IDX))
. When I plot the computation time needed I can see that when the indexed part is more than %75 of the data it takes actually more time. What can I do to overcome this, or push the performance gain to say %85?
EDIT: I am adding the results after switching to logical indexing, obviously better. It seems there is a performance loss on the low %10 end however (visible if you view the images on top of each other)
I'm pretty sure what's happening here is your indexing scheme is taking a small amount of time. No matter what method you choose, it will take some time, although there are better methods. Logical is always better than using a find statement, but perhaps even better is to just use the index directly. Here's some sample code I used to test stuff, along with the results. Note, I ran this using 2010, I think there's some kind of optimization that occurs in the higher values, but I'm not sure exactly what's going on there... It is clear that direct indexing seems a bit faster than using a logical, and should be much faster than some kind of a find statement.
function time_test
time_full_test=zeros(1e3,1);
time_part_test=zeros(1e3,1);
time_direct_indexing_full=zeros(1e3,1);
time_direct_indexing_part=zeros(1e3,1);
data=rand(1e5,1);
for i=1:1e3
time_full_test(i)=complex_stuff(data);
time_part_test(i)=complex_stuff(data,i*100);
time_direct_indexing_full(i)=complex_stuff2(data);
time_direct_indexing_part(i)=complex_stuff2(data,i*100);
end
figure;plot(time_full_test);hold all;plot(time_part_test);plot(time_direct_indexing_full);plot(time_direct_indexing_part)
legend('Full Time Logic','Part Time Logic','Full Time Direct','Part Time Direct')
function time=complex_stuff(input,max_val)
tic
if ~exist('max_val','var')
mask=true(size(input));
else
mask=false(size(input));
mask(1:max_val)=true;
end
sin(input(mask).^2/4356.342).^63/345;
time=toc;
function time=complex_stuff2(input,max_val)
tic
if ~exist('max_val','var')
max_val=length(input);
end
sin(input(1:max_val).^2/4356.342).^63/345;
time=toc;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With