Optimizing Euclidian distance code
조회 수: 4 (최근 30일)
이전 댓글 표시
Dear all,
I am trying to optimize my code for calulating the minimum distance to the origin, given a set of 3D points specified by column vectors x, y and z. I use profile to obtain the computing time.
x = [25; 40; 63];
y = [12; 34; 56];
z = [30; 55; 77];
profile on
d = minDistance(x,y,z);
profile viewer
function d = minDistance(x,y,z)
% Given a set of 3D points specified by column vectors x,y,z, this
% function computes the minimum distance to the origin
% preallocate
d = zeros(length(x),1);
% compute distance for every point
for k = 1:length(x)
d(k) = sqrt(x(k)^2 + y(k)^2 + z(k)^2);
end
% get minimum distance
d = min(d)
end
I'm aware of the fact that the biggest time consumer is the for-loop, but I don't know how to optimize such a loop.
Anyone who can explain how such an optimization is done?
Thanks on beforehand!
댓글 수: 0
답변 (1개)
Ameer Hamza
2020년 4월 15일
편집: Ameer Hamza
2020년 4월 15일
Try this
function d = minDistance(x,y,z)
d = min(vecnorm([x y z], 2, 2));
end
댓글 수: 2
Ameer Hamza
2020년 4월 16일
Yes, you are correct. I tried a bit, and vecnorm() seems to be consuming a lot of time because it is a quite general function and its internal implementation takes some time to decide which vector norm to take and along which dimension. Also, my code spends some time to concatenate [x y z], although I think it does not make a copy of these vectors (because MATLAB use copy on write), It will still take some time to concatenate them.
Note that you can improve the performance of your current code by not using sqrt() at each iteration. Instead, just take sqrt() at the end.
function d = minDistance2(x,y,z)
% Given a set of 3D points specified by column vectors x,y,z, this
% function computes the minimum distance to the origin
% preallocate
d = zeros(length(x),1);
% compute distance for every point
for k = 1:length(x)
d(k) = x(k)^2 + y(k)^2 + z(k)^2;
end
% get minimum distance
d = sqrt(min(d));
end
I tried a bit further and found that the following code can give you comparable performance while being concise. Note that it requires that you pass it a matrix X instead of 3 vectors.
function d = minDistance(X)
d = sqrt(min(sum(X.^2,2)));
end
The following code shows that both codes almost have comparable performance. Note that I used input vectors of size 10000x1.
x = rand(1000000,1);
y = rand(1000000,1);
z = rand(1000000,1);
X = [x y z];
t1 = timeit(@() minDistance(X))
t2 = timeit(@() minDistance2(x,y,z))
function d = minDistance(X)
d = sqrt(min(sum(X.^2,2)));
end
function d = minDistance2(x,y,z)
% Given a set of 3D points specified by column vectors x,y,z, this
% function computes the minimum distance to the origin
% preallocate
d = zeros(length(x),1);
% compute distance for every point
for k = 1:length(x)
d(k) = x(k)^2 + y(k)^2 + z(k)^2;
end
% get minimum distance
d = sqrt(min(d));
end
Result
t1 =
0.0045
t2 =
0.0041
참고 항목
카테고리
Help Center 및 File Exchange에서 Surrogate Optimization에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!