필터 지우기
필터 지우기

Which of the two methods is faster

조회 수: 2 (최근 30일)
Kaushlender Kumar
Kaushlender Kumar 2019년 2월 19일
편집: Kaushlender Kumar 2019년 2월 23일
x1 = single(10);
x2 = single(30);
n = 7;
[r1 r2] = ratio(x1,x2,n);
[r1 r2]
function [r1 r2] = ratio(x1,x2,n)
tic
r1 = zeros(n,1,'single');
r2 = r1;
for k = 1:n
r1(k) = x1^(2^k)/x2^(2^k);
r2(k) = (x1/x2)^(2^k);
end
toc
end
Present the results from running the above driver program. Since
(1) is true, one can conclude that the two methods r1 and r2 should give
identical results. Explain why this is not true as the values of k
increase.

답변 (1개)

Pranjal Priyadarshi
Pranjal Priyadarshi 2019년 2월 22일
There is no evidence that vectorized code is faster in general.
If a build-in function can be applied to a complete array, a vectorization is much faster than a loop approach. When large temporary arrays are required, the benefits of the vectorization can be dominated by the expensive allocation of the memory, when it does not match into the processor cache.
A secondary effect of vectorizing is that the code looks clearer.
As the value of k increases the benefit that vectorization provides diminishes as it is no more possible to cache the large array. In order to be time efficient, we can better use data structures present in the MATLAB library like tall arrays or datastores.

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by