Computing time for .^(1/2) or .^(1/p)
Show older comments
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)
Accepted Answer
More Answers (2)
James Tursa
on 29 Apr 2019
Edited: James Tursa
on 29 Apr 2019
0 votes
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
on 29 Apr 2019
Edited: Walter Roberson
on 29 Apr 2019
0 votes
https://www.felixcloutier.com/x86/sqrtpd (hardware square root)
1 Comment
KALYAN ACHARJYA
on 29 Apr 2019
oK sir
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!