Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gnu Scientific Library poor preformance compared to libc

Tags:

c++

gsl

I've implemented a simple program to measure sin calculation performance by gnu scientific library and libc. Here is a source code :

#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;
}

Here is a result : STD : 57.8869 ms GSL : 106.787 ms

So is it OK for GSL to be slower than libc?

like image 774
Dmitry Avatar asked Mar 07 '26 22:03

Dmitry


1 Answers

GSL uses software sin (even when a CPU sin is available), and it can give you error estimates too: gsl_sf_sin is just a wrapper currently to gsl_sf_sin_e (which gives error estimates). This routine is not optimized too much for speed.

On the other hand, libc sin is usually optimized pretty well for speed.

Furthermore, your benchmark may be flawed, as sin might be optimized out by the compiler, if optimization is enabled.

like image 86
geza Avatar answered Mar 09 '26 12:03

geza



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!