c++ - 与 glibc 相比,Gnu Scientific Library 性能不佳

标签 c++ gsl

我已经实现了一个简单的程序来测量 gnu 科学库和 libc 的 sin 计算性能。这是源代码:

#include <iostream>
#include <vector>
#include <math.h>
#include <chrono>
#include <list>
#include "gsl/gsl_sf_trig.h"

int main()
{
    std::uint32_t numStepsToCalculate = 1000000;
    const double startX = 0.0;
    const double finishX = M_PI*2;
    const double stepX  = (finishX - startX)/numStepsToCalculate;

    double currentX = startX;
    std::list<double> res;

    auto startT = std::chrono::steady_clock::now();
    while ( currentX <= finishX ) {
        res.push_back( sin ( currentX ) );
        currentX += stepX;
    }
    auto endT = std::chrono::steady_clock::now();

    auto diffT = endT - startT;
    std::cout << "STD : " << std::chrono::duration <double, std::milli> (diffT).count() << " ms" << std::endl;
    std::cout << "STD res size " << res.size() << std::endl;

    std::list<double> resOpt;
    currentX = startX;
    auto startTopt = std::chrono::steady_clock::now();
    while ( currentX <= finishX ) {
        resOpt.push_back( gsl_sf_sin ( currentX ) );
        currentX += stepX;
    }

    auto endTopt = std::chrono::steady_clock::now();

    auto diffTopt = endTopt - startTopt;

    std::cout << "GSL : " << std::chrono::duration <double, std::milli> (diffTopt).count() << " ms" << std::endl;
    std::cout << "GSL res size " << resOpt.size() << std::endl;
    return 0;
}

这是一个结果: 标准:57.8869 毫秒 GSL:106.787 毫秒

那么 GSL 可以比 libc 慢吗?

最佳答案

GSL 使用软件 sin(即使 CPU sin 可用),它也可以给你错误估计:gsl_sf_sin 目前只是 gsl_sf_sin_e 的包装器(它给出误差估计)。此例程并未针对速度进行过多优化。

另一方面,libc sin 通常针对速度进行了很好的优化。

此外,您的基准测试可能存在缺陷,因为如果启用了优化,sin 可能会被编译器优化掉。

关于c++ - 与 glibc 相比,Gnu Scientific Library 性能不佳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51061363/

相关文章:

C++ 舍入问题

c++ - 有没有办法不用/UNICODE编译就可以使用MFC的CEdit的函数 "ShowBalloonTip"?

c++ - 使用多个 slider 在自定义 QWidget 中映射 QSlider::valueChanged 信号

c++ - 修改 Makefile 添加 GSL 库

c - GSL 特征值顺序

c - 将图像加载到 GSL 矩阵

c++ - 使用 Visual Studio 10 编译 x64 EMMS

c++ - C 与 C++ 中的 struct 和 typedef

c++ - vector::删除()不工作

c - 如何使用 gsl 计算多项式回归数据点?