Why is std::sin() and std::cos() slower than sin() and cos()?

前端 未结 4 1808
小蘑菇
小蘑菇 2021-01-01 13:14

Test code:

#include 
#include 

const int N = 4096;
const float PI = 3.1415926535897932384626;

float cosine[N][N];
float sine[N][         


        
4条回答
  •  孤城傲影
    2021-01-01 13:47

    I did some measurements using clang with -O3 optimization, running on an Intel Core i7. I found that:

    • std::sin on float has the same cost as sinf
    • std::sin on double has the same cost as sin
    • The sin functions on double are 2.5x slower than on float (again, running on an Intel Core i7).

    Here is the full code to reproduce it:

    #include 
    #include 
    #include 
    
    template
    struct Timer
    {
        using rep = typename Clock::rep;
        using time_point = typename Clock::time_point;
        using resolution = typename Clock::duration;
    
        Timer(rep& duration) :
        duration(&duration) {
            startTime = Clock::now();
        }
        ~Timer() {
            using namespace std::chrono;
            *duration = duration_cast(Clock::now() - startTime).count();
        }
    private:
    
        time_point startTime;
        rep* duration;
    };
    
    template
    void testSin(F sin_func) {
      using namespace std;
      using namespace std::chrono;
      high_resolution_clock::rep duration = 0;
      T sum {};
      {
        Timer t(duration);
        for(int i=0; i<100000000; ++i) {
          sum += sin_func(static_cast(i));
        }
      }
      cout << duration << endl;
      cout << "  " << sum << endl;
    }
    
    int main() {
      testSin ([] (float  v) { return std::sin(v); });
      testSin ([] (float  v) { return sinf(v); });
      testSin([] (double v) { return std::sin(v); });
      testSin([] (double v) { return sin(v); });
      return 0;
    }
    

    I'd be interested if people could report, in the comments on the results on their architectures, especially regarding float vs. double time.

提交回复
热议问题