Computing time for .^(1/2) or .^(1/p)
이전 댓글 표시
Hi,
I realised that computing the square root element-wise of a matrix using .^(1/2) is faster than computing the pth root whatever the value of p :
I found (with the provided code below):
Averaged time for A.^(1/2): 0.0337 s
Averaged time for A.^(1/3): 0.0972 s
Averaged time for A.^(1/5): 0.0946 s
Does anyone know why ? (I am clearly not an expert in algorithm complexity and optimization, but I'd like to know if there is a special case of the square root implemented in the function (.^), and eventually to understand how it works in a simple way).
Thanks in advance :) !
clear all
close all
clc
% Data
A = randn(1024,1024);
% Averaged time to Compute the square root of each element in A
t_squareroot = 0;
for u = 1 : 50
tic
B = A.^(1/2);
t_squareroot = t_squareroot + toc/50;
end
disp('Averaged time for A.^(1/2):')
disp(t_squareroot)
% Averaged time to Compute the cubic root of each element in A
t_cubicroot = 0;
for u = 1 : 50
tic
C = A.^(1/3);
t_cubicroot = t_cubicroot + toc/50;
end
disp('Averaged time for A.^(1/3)')
disp(t_cubicroot)
% Averaged time to Compute the 5th root of each element in A
t_5throot = 0;
for u = 1 : 50
tic
D = A.^(1/5);
t_5throot = t_5throot + toc/50;
end
disp('Averaged time for A.^(1/5)')
disp(t_5throot)
채택된 답변
추가 답변 (2개)
James Tursa
2019년 4월 29일
편집: James Tursa
2019년 4월 29일
0 개 추천
I am unaware of any documentation on how TMW has chosen to implement their rooting/power algorithms, but it would not surprise me that they use a custom fast algorithm for sqrt vs a slower more generic algorithm for other powers & roots. E.g., one could use a rational function approximation for the mantissa combined with direct exponent bit manipulation for square root, and then something else for all the other powers. (This is just an example of what could be done ... not necessarily my guess at what the library code that MATLAB uses does do).
Walter Roberson
2019년 4월 29일
편집: Walter Roberson
2019년 4월 29일
0 개 추천
https://www.felixcloutier.com/x86/sqrtpd (hardware square root)
카테고리
도움말 센터 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!