I made a function with static initiation at the beginning in order to calculate values only the first time the function is called, but then the function runs slower.
It is used VS2017 on windows10 (I5 processor 3.5Ghz)
Here is the result:

const static double: 20ms static double: 16ms double only: 5.1ms sum only: 5ms
Here is the code. function1 is the slowest one.
#include <iostream>
#include <vector>
#include <chrono>
using namespace std;
//=============== TIMER PART =================
#ifdef __linux__
#include <unistd.h> //usleep()
typedef std::chrono::system_clock t_clock; //try to use high_resolution_clock on new linux x64 computer!
#else
typedef std::chrono::high_resolution_clock t_clock;
#pragma warning(disable:4996)
#endif
std::chrono::time_point<t_clock> start_time, stop_time = start_time; char null_char = '\0';
void timer(const char *title = 0, int data_size = 1) { stop_time = t_clock::now(); double us = (double)chrono::duration_cast<chrono::microseconds>(stop_time - start_time).count(); if (title) printf("%s time = %7lgms = %7lg MOPs\n", title, (double)us*1e-3, (double)data_size / us); start_time = t_clock::now(); }
//=============== TIMER PART =================
double f1(double x)
{
const static double a=100.1,b=3+sqrt(a),c=10+sin(b/45)+cos(b/45+a),d=c+tan(b/105.3);
return x+d;
}
double f2(double x)
{
static double a=100.1,b=3+sqrt(a),c=10+sin(b/45)+cos(b/45+a),d=c+tan(b/105.3);
return x+d;
}
double f3(double x)
{
double a=100.1,b=3+sqrt(a),c=10+sin(b/45)+cos(b/45+a),d=c+tan(b/105.3);
return x+d;
}
double f4(double x)
{
return x+17.33;
}
int main()
{
int i,top=500000;
double sum=0.0;
timer();
for (i=0;i<top;i++)
{
sum+=f1(i*10.0);sum+=f1(i*10.0);sum+=f1(i*10.0);sum+=f1(i*10.0);sum+=f1(i*10.0);
sum+=f1(i*10.0);sum+=f1(i*10.0);sum+=f1(i*10.0);sum+=f1(i*10.0);sum+=f1(i*10.0);
}
timer("const static double",top*10);
for (i=0;i<top;i++)
{
sum+=f2(i*10.0);sum+=f2(i*10.0);sum+=f2(i*10.0);sum+=f2(i*10.0);sum+=f2(i*10.0);
sum+=f2(i*10.0);sum+=f2(i*10.0);sum+=f2(i*10.0);sum+=f2(i*10.0);sum+=f2(i*10.0);
}
timer("static double",top*10);
for (i=0;i<top;i++)
{
sum+=f3(i*10.0);sum+=f3(i*10.0);sum+=f3(i*10.0);sum+=f3(i*10.0);sum+=f3(i*10.0);
sum+=f3(i*10.0);sum+=f3(i*10.0);sum+=f3(i*10.0);sum+=f3(i*10.0);sum+=f3(i*10.0);
}
timer("double",top*10);
for (i=0;i<top;i++)
{
sum+=f4(i*10.0);sum+=f4(i*10.0);sum+=f4(i*10.0);sum+=f4(i*10.0);sum+=f4(i*10.0);
sum+=f4(i*10.0);sum+=f4(i*10.0);sum+=f4(i*10.0);sum+=f4(i*10.0);sum+=f4(i*10.0);
}
timer("sum only",top*10);
cout<<sum<<endl;
cout<<"\n=== FIN ==="<<endl;
getchar();return 1;
}
Because the compiler must ensure that static local variable initialization is performed only once even in a multi-threaded environment.
So local static variable initialization is surrounded by a thread safe guard which is expensive. That is the reason why the use of static local variable is discouraged.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With