#除法,乘法,移位耗时对比
发现移位和乘法比除法要省3-5倍时间,移位相对而言是最省时间的
auto time_start = std::chrono::system_clock::now();
int n = 100000;
double d ;
for (int i = 0; i < 1000000; i++)
{
d = n / 2;
}
std::chrono::duration<double> time_spend = std::chrono::system_clock::now() - time_start;
double time_cost = time_spend.count() * 1000;
cout<<"div cost "<<time_cost<<" ms"<<endl;
time_start = std::chrono::system_clock::now() ;
for (int i = 0; i < 1000000; i++)
{
d = n * 0.5f;
}
time_spend = std::chrono::system_clock::now() - time_start;
time_cost = time_spend.count() * 1000;
cout<<"muutiple cost "<<time_cost<<" ms"<<endl;
time_start = std::chrono::system_clock::now() ;
for (int i = 0; i < 1000000; i++)
{
d = n >>1;
}
time_spend = std::chrono::system_clock::now() - time_start;
time_cost = time_spend.count() * 1000;
cout<<"shit cost "<<time_cost<<" ms"<<endl;
return 0;
输出结果如下:
div cost 0.000139 ms
muutiple cost 3.3e-05 ms
shit cost 2.8e-05 ms