简体   繁体   English

Math.Sqrt() 的时间复杂度?

[英]Time complexity of Math.Sqrt()?

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):我知道下面的部分是 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.但我无法弄清楚Math.Sqrt()的复杂性是什么。

You can consider it O(1):你可以认为它是 O(1):

In other words, Math.Sqrt() translates to a single floating point machine code instruction换句话说,Math.Sqrt() 转换为单个浮点机器代码指令

source: c# Math.Sqrt Implementation来源: c# Math.Sqrt 实现

As mentioned by BlackBear , the Math.Sqrt implementation translates to a to a single floating point machine code instruction (fsqrt).正如所提到BlackBear ,所述Math.Sqrt执行转换为一个给单个浮点机器代码指令(FSQRT)。 The number of cycles of this instruction is bounded ( here are some examples).该指令的周期数是有界的(这里有一些例子)。 And that means its complexity is O(1).这意味着它的复杂度是 O(1)。

That is only true, because we use a limited number of floating-point values.那是正确的,因为我们使用了有限数量的浮点值。 The "actual" complexity of that operation depends on the number of bits of the input.该操作的“实际”复杂度取决于输入的位数。 Here you can find a list of the complexity of basic arithmetic functions. 在这里您可以找到基本算术函数的复杂性列表。 According to that list the squareroot function has the complexity of the multiplication function (O(n log n) for two n-digit numbers).根据该列表,平方根函数具有乘法函数的复杂性(两个 n 位数的 O(n log n))。

You said, that you assume addition and multiplication function have complexity O(1).你说,你假设加法和乘法函数的复杂度为 O(1)。 That means, you can assume that the squareroot function, allthough much slower, has complexity O(1), too.这意味着,您可以假设平方根函数虽然慢得多,但也具有 O(1) 复杂度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM