Is it better to use sqrt or ^(1/2) ?

24 ビュー (過去 30 日間)
Andre Ged
Andre Ged 2016 年 3 月 15 日
コメント済み: Walter Roberson 2023 年 9 月 8 日
In order to have better performance in terms of computational time, is it better to use sqrt or ^(1/2) ?

採用された回答

KSSV
KSSV 2016 年 3 月 15 日
編集済み: per isakson 2017 年 6 月 3 日
clc; clear all
N = 10:10:100000;
t_sqrt = zeros(length(N),1) ;
t_pow = t_sqrt ;
for i = 1:length(N)
k = rand(N(i),1) ;
t1 = tic ;
k1 = sqrt(k) ;
t_sqrt(i) = toc(t1) ;
%
t2 = tic ;
k2 = k.^0.5 ;
t_pow(i) = toc(t2) ;
end
figure
plot(t_sqrt,'r') ;
hold on
plot(t_pow,'b') ;
legend('sqrt','power')
You may check yourself.....power (^) is taking less time.
  2 件のコメント
NAIMA Khatir
NAIMA Khatir 2017 年 6 月 3 日
your are a genuis
Walter Roberson
Walter Roberson 2017 年 6 月 3 日
My tests suggest that sqrt() is faster, except maybe on some very small vectors.
Note: it will take a while to execute the below as it tests over a range of sizes.
N = round(logspace(2,7,500));
t_sqrt = zeros(length(N),1) ; t_sqrt1 = t_sqrt; t_pow = t_sqrt; t_pow1 = t_sqrt;
data = rand(1,max(N));
for i = 1 : length(N); k = data(1,1:N(i)); t_sqrt(i) = timeit(@() sqrt(k),0); t_pow(i) = timeit(@() k.^(1/2), 0); end
for i = 1 : length(N); k = data(1,1:N(i)); t_sqrt1(i) = timeit(@() sqrt(k),0); end; for i = 1 : length(N); k = data(1,1:N(i)); t_pow1(i) = timeit(@() k.^(1/2),0); end
plot(N,t_sqrt,'r-',N,t_sqrt1,'r--', N,t_pow,'b-', N,t_pow1, 'b--')
legend({'sqrt, combined loop', 'sqrt, separate loops', 'pow, combined loops', 'pow, separate loop'} )
I originally had only the combined loop, and smaller upper bounds, but I noticed some oddities in the timings for which the timings were sometimes shorter in synchronized ways. That suggested that some iterations might happen to execute faster than others by chance, so I decided to also run the loops independently to see whether the timing oddities stayed with the array sizes or were instead independent between the separated attempts. The tests did end up suggesting that the timing oddities were related to array sizes. Likely at the point where MATLAB starts handing over the work to BLAS or MLK or LINPACK or whatever, the iterations get relatively faster.

サインインしてコメントする。

その他の回答 (1 件)

Amarpreet
Amarpreet 2023 年 9 月 8 日
Both these operations result in different values when operated on a matrix, so that may also be considered while using the functions.sqrt gives square root of each element, while the pow function operates on the whole matrix.
  2 件のコメント
Sam Chak
Sam Chak 2023 年 9 月 8 日
For the computation of the matrix square root, have you compared the speed with sqrtm() function as well?
M = diag([4 9 25])
M = 3×3
4 0 0 0 9 0 0 0 25
Mr = sqrtm(M)
Mr = 3×3
2 0 0 0 3 0 0 0 5
Walter Roberson
Walter Roberson 2023 年 9 月 8 日
N = 5:5:100;
t_sqrt = zeros(length(N),1) ; t_sqrt1 = t_sqrt; t_pow = t_sqrt; t_pow1 = t_sqrt;
data = rand(max(N));
for i = 1 : length(N); k = data(1:N(i),1:N(i)); t_sqrt(i) = timeit(@() sqrtm(k),0); t_pow(i) = timeit(@() k^(1/2), 0); end
for i = 1 : length(N); k = data(1:N(i),1:N(i)); t_sqrt1(i) = timeit(@() sqrtm(k),0); end; for i = 1 : length(N); k = data(1:N(i),1:N(i)); t_pow1(i) = timeit(@() k^(1/2),0); end
plot(N,t_sqrt,'r-',N,t_sqrt1,'r--', N,t_pow,'b-', N,t_pow1, 'b--')
legend({'sqrt, combined loop', 'sqrt, separate loops', 'pow, combined loops', 'pow, separate loop'} )

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by