c++ - 实现上的巨大差异?

标签 c++ performance boost time-measurement

我正在为分布编写一些功能,并使用正态分布在我的实现和 C++ Boost 之间运行测试。

给定概率密度函数(pdf:http://www.mathworks.com/help/stats/normpdf.html)

我是这样写的:

double NormalDistribution1D::prob(double x) {
    return (1 / (sigma * (std::sqrt(boost::math::constants::pi<double>()*2))))*std::exp((-1 / 2)*(((x - mu) / sigma)*((x - mu) / sigma)));
}

将我的结果与使用 C++ Boost 的方式进行比较:

    boost::math::normal_distribution <> d(mu, sigma);
    return boost::math::pdf(d, x);

我不是很惊讶 - 我的版本用了 44278 纳秒, boost 只有 326

所以我玩了一下,在我的 NormalDistribution1D-Class 中编写了方法 probboost 并比较了所有三个:

void MATTest::runNormalDistribution1DTest1() {
    double mu = 0;
    double sigma = 1;
    double x = 0;

    std::chrono::high_resolution_clock::time_point tn_start = std::chrono::high_resolution_clock::now();
    NormalDistribution1D *n = new NormalDistribution1D(mu, sigma);
    double nres = n->prob(x);
    std::chrono::high_resolution_clock::time_point tn_end = std::chrono::high_resolution_clock::now();

    std::chrono::high_resolution_clock::time_point tdn_start = std::chrono::high_resolution_clock::now();
    NormalDistribution1D *n1 = new NormalDistribution1D(mu, sigma);
    double nres1 = n1->probboost(x);
    std::chrono::high_resolution_clock::time_point tdn_end = std::chrono::high_resolution_clock::now();

    std::chrono::high_resolution_clock::time_point td_start = std::chrono::high_resolution_clock::now();
    boost::math::normal_distribution <> d(mu, sigma);
    double dres = boost::math::pdf(d, x);
    std::chrono::high_resolution_clock::time_point td_end = std::chrono::high_resolution_clock::now();

    std::cout << "Mu : " << mu << "; Sigma: " << sigma << "; x" << x << std::endl;
    if (nres == dres) {
        std::cout << "Result" << nres << std::endl;
    } else {
        std::cout << "\033[1;31mRes incorrect: " << nres << "; Correct: " << dres << "\033[0m" << std::endl;
    }


    auto duration_n = std::chrono::duration_cast<std::chrono::nanoseconds>(tn_end - tn_start).count();
    auto duration_d = std::chrono::duration_cast<std::chrono::nanoseconds>(td_end - td_start).count();
    auto duration_dn = std::chrono::duration_cast<std::chrono::nanoseconds>(tdn_end - tdn_start).count();

    std::cout << "own boost: " << duration_dn << std::endl;
    if (duration_n < duration_d) {
        std::cout << "Boost: " << (duration_d) << "; own implementation: " << duration_n << std::endl;
    } else {
        std::cout << "\033[1;31mBoost faster: " << (duration_d) << "; than own implementation: " << duration_n << "\033[0m" << std::endl;
    }
}

结果是(正在编译和运行 checking-Method 3 次)

own boost: 1082 Boost faster: 326; than own implementation: 44278

own boost: 774 Boost faster: 216; than own implementation: 34291

own boost: 769 Boost faster: 230; than own implementation: 33456

现在这让我很困惑: 类中的方法怎么可能比直接调用的语句长 3 倍?

我的编译选项:

g++ -O2   -c -g -std=c++11 -MMD -MP -MF "build/Debug/GNU-Linux-x86/main.o.d" -o build/Debug/GNU-Linux-x86/main.o main.cpp

g++ -O2    -o ***Classes***

最佳答案

首先,您使用 new 动态分配对象:

NormalDistribution1D *n = new NormalDistribution1D(mu, sigma);
double nres = n->prob(x);

如果您像使用 boost 一样进行操作,那么仅此一项就足以获得相同(或相当)的速度:

NormalDistribution1D n(mu, sigma);
double nres = n.prob(x);

现在,我不知道你在 NormalDistribution1D::prob() 中拼写表达式的方式是否重要,但我怀疑将它写成更“优化”的方式,因为像这样的算术表达式是编译器可以很好地优化的东西。如果您使用 --ffast-math 开关,它可能会变得更快,这将为编译器提供更多的优化自由。

此外,如果 double NormalDistribution1D::prob(double x) 的定义在另一个编译单元(另一个 .cpp 文件)中,编译器将无法内联它,这也会产生明显的开销(可能慢两倍或更少)。在 boost 中,几乎所有的东西都是在头文件中实现的,所以当编译器看起来合适时,内联总是会发生。如果使用 gcc 的 -flto 开关进行编译和链接,就可以解决这个问题。

关于c++ - 实现上的巨大差异?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31841824/

相关文章:

c++ - 跟踪 boost::spirit 的位置

c++ - boost 日志 : Support file name and line number

c++ - 指向抽象基类的指针需要一些转换吗? C++

vector 的 C++ vector

c++ - vector::push_back() 是否在进行浅拷贝以及如何解决这个问题

c++ - 由于换行字符不同的结果

java - java中的基准测试是否适合比较模算术运算?

java - Java 应用程序性能是否取决于将变量传递给方法?

Javascript 模块模式内存占用和性能

c++ - 如何划分boost::optional<double>?