How can I find the complexity of this function?
private double EuclideanDistance(MFCC.MFCCFrame vec1, MFCC.MFCCFrame vec2)
{
double Distance = 0.0;
for (int K = 0; K < 13; K++)
Distance += (vec1.Features[K] - vec2.Features[K]) * (vec1.Features[K] - vec2.Features[K]);
return Math.Sqrt(Distance);
}
I know that the below section is O(1):
double Distance = 0.0;
for (int K = 0; K < 13; K++)
Distance += (vec1.Features[K]-vec2.Features[K])*(vec1.Features[K]-vec2.Features[K]);
But I can't figure out what the complexity of Math.Sqrt()
is.
There is no algorithm which would compute a square root in constant time.
sqrt() is "slow," not slow. Fundamentally, the operation is more complex than an addition or division.
Math. sqrt() returns the square root of a value of type double passed to it as argument. If the argument is NaN or negative, then the result is NaN.
The sqrt() function takes a single argument (in double ) and returns its square root (also in double ). The sqrt() function is defined in math. h header file. To find the square root of int , float or long double data types, you can explicitly convert the type to double using cast operator.
You can consider it O(1):
In other words, Math.Sqrt() translates to a single floating point machine code instruction
source: c# Math.Sqrt Implementation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With