std::tgamma 的时间复杂度是多少?

标准规定了吗?如果不是,是否有可以用作指导的通用实现?

最佳答案

不变。至少在我的机器上。

我写了一个程序来对tgamma进行基准测试,它的确非常快,而且随着值的增加超过一个小点,似乎并不需要增加时间。请注意,我必须循环功能10,000,000 friggin次才能获得可观的基准。

结果:

D: 0.845001, gamma: 1.11728, clock: 795
D: 1.27208, gamma: 0.902123, clock: 593
D: 2.0245, gamma: 1.01061, clock: 655
D: 3.14767, gamma: 2.30167, clock: 1092
D: 4.33883, gamma: 9.3294, clock: 1217
D: 5.45709, gamma: 48.8554, clock: 1201
D: 6.58879, gamma: 337.785, clock: 1217
D: 10.7151, gamma: 1.86412e+06, clock: 1076
D: 14.3596, gamma: 1.5953e+10, clock: 1077
D: 26.477, gamma: 7.30237e+25, clock: 1123
D: 28.298, gamma: 2.92832e+28, clock: 1123
D: 39.8976, gamma: 1.39983e+46, clock: 1077
D: 75.0072, gamma: 3.41163e+107, clock: 1107
D: 98.9705, gamma: 8.23214e+153, clock: 1155
D: 196.018, gamma: inf, clock: 826
D: 212.68, gamma: inf, clock: 812
D: 405.715, gamma: inf, clock: 842
D: 447.305, gamma: inf, clock: 858
D: 859.709, gamma: inf, clock: 827
D: 1296.06, gamma: inf, clock: 827
D: -0.83337, gamma: -6.68084, clock: 2246
D: -1.4104, gamma: 2.61483, clock: 1888
D: -2.19592, gamma: -2.2539, clock: 1934
D: -2.99066, gamma: -18.0574, clock: 1825
D: -3.08634, gamma: 1.75148, clock: 2091
D: -5.736, gamma: 0.00964749, clock: 2215
D: -6.30378, gamma: -0.00300647, clock: 2324
D: -11.8326, gamma: 1.99199e-08, clock: 2138
D: -17.1485, gamma: 1.28265e-14, clock: 2074
D: -24.56, gamma: -8.54102e-25, clock: 2106
D: -39.4499, gamma: 2.9751e-47, clock: 2138
D: -76.053, gamma: -7.97988e-111, clock: 2090
D: -126.574, gamma: -8.44671e-213, clock: 2122
D: -176.617, gamma: -6.96633e-322, clock: 3463
D: -254.058, gamma: -0, clock: 1513
D: -419.736, gamma: 0, clock: 1607
D: -809.613, gamma: 0, clock: 1591
D: -1578.05, gamma: -0, clock: 1544
D: -2063.04, gamma: 0, clock: 1514
D: -3293.61, gamma: 0, clock: 1669

码:
#include <iostream>
#include <cmath>

// Prevent optimizations
volatile long double d;
volatile long double my_gamma;

double drand()
{
  return double(rand()) / RAND_MAX;
}

int main (void)
{
  d = .5 + drand() / 2;

  for (int i = 0; i < 20; ++i, d *= 1 + drand())
  {
    clock_t start = clock();
    for (int j = 0; j < 10000000; ++j)
    {
      my_gamma = tgamma(d);
    }
    clock_t stop = clock();

    std::cout << "D: " << d << ", gamma: " << my_gamma << ", clock: " << stop - start << std::endl;
  }

  d = -1 + drand() / 2;

  for (int i = 0; i < 20; ++i, d *= 1 + drand())
  {
    clock_t start = clock();
    for (int j = 0; j < 10000000; ++j)
    {
      my_gamma = tgamma(d);
    }
    clock_t stop = clock();

    std::cout << "D: " << d << ", gamma: " << my_gamma << ", clock: " << stop - start << std::endl;
  }

  return 0;
}

关于c++ - std::tgamma的复杂度是多少?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31197064/

10-09 06:39