I'm working with algorithms using a large amount of maths functions, and recently we ported the code under g++ 4.8.2 on an Ubuntu system from a Solaris platform.
Surprisingly, some of the algorithms were taking way much time than before. The reason behind is that the std::tan()
function is two times longer than doing std::sin()/std::cos()
.
Replacing the tan by sin/cos has considerably reduced the computing time for the same results. I wonder why there is such a difference. Is it because of the implementation of the standard library ? Shouldn't the tan function be more effective ?
I wrote a program to check the time of the functions :
#include <cmath>
#include <iostream>
#include <chrono>
int main(int argc, char * argv[])
{
using namespace std::chrono;
auto start_tan = system_clock::now();
for (int i = 0; i < 50000; ++i)
{
const double & a = static_cast<double>(i);
const double & b = std::tan(a);
}
auto end_tan = system_clock::now();
auto elapsed_time_tan = end_tan - start_tan;
std::cout << "tan : ";
std::cout << elapsed_time_tan.count() << std::endl;
auto start_sincos = system_clock::now();
for (int i = 0; i < 50000; ++i)
{
const double & a = static_cast<double>(i);
const double & b = std::sin(a) / std::cos(a);
}
auto end_sincos = system_clock::now();
auto elapsed_time_sincos = end_sincos - start_sincos;
std::cout << "sincos : " << elapsed_time_sincos.count() << std::endl;
}
And indeed, in the output I have the following time without optimisation :
tan : 8319960
sincos : 4736988
And with optimisation (-O2) :
tan : 294
sincos : 120
If anyone has any idea about this behaviou
Aucun commentaire:
Enregistrer un commentaire