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 behaviour.
EDIT
I modified the program according to @Basile Starynkevitch response :
#include <cmath>
#include <iostream>
#include <chrono>
int main(int argc, char * argv[])
{
using namespace std::chrono;
if (argc != 2)
{
std::cout << "Need one and only argument : the number of iteration." << std::endl;
return 1;
}
int nb_iter = std::atoi(argv[1]);
std::cout << "Number of iteration programmed : " << nb_iter << std::endl;
double tan_sum = 0.0;
auto start_tan = system_clock::now();
for (int i = 0; i < nb_iter; ++i)
{
const double & a = static_cast<double>(i);
const double b = std::tan(a);
tan_sum += b;
}
auto end_tan = system_clock::now();
auto elapsed_time_tan = end_tan - start_tan;
std::cout << "tan : " << elapsed_time_tan.count() << std::endl;
std::cout << "tan sum : " << tan_sum << std::endl;
double sincos_sum = 0.0;
auto start_sincos = system_clock::now();
for (int i = 0; i < nb_iter; ++i)
{
const double & a = static_cast<double>(i);
const double b = std::sin(a) / std::cos(a);
sincos_sum += b;
}
auto end_sincos = system_clock::now();
auto elapsed_time_sincos = end_sincos - start_sincos;
std::cout << "sincos : " << elapsed_time_sincos.count() << std::endl;
std::cout << "sincos sum : " << sincos_sum << std::endl;
}
And now as result I get similar time for -O2
only :
tan : 8345021
sincos : 7838740
But still the difference with -O2 -mtune=native
, but faster indeed :
tan : 5426201
sincos : 3721938
I won't user -ffast-math
because I need to keep IEEE compliance.