有关 C++里一些函数的计算速度测试
反三角比三角计算快,tanh计算比exp快...
atan2比atan慢这么多。 好神奇
#include<iostream>
#include <fstream>
#include <algorithm>
#include <time.h>
#include<windows.h>
using namespace std;
int main()
{
double i, j, k, count = 1000;
double tmp, time;
clock_t start_time = clock();
DWORD dwEnd, dwStart = GetTickCount();
for (k = 0; k < count; ++k)
{
for (i = 0; i < 256; ++i)
{
for (j = 1; j < 256; ++j)
{
// tmp = atan(double(i) / j);
tmp = rand();
// cout << tmp << endl;
}
}
}
dwEnd = GetTickCount();
cout << "The rand run time is:" << (dwEnd - dwStart) << "ms!" << endl;//输出运行时间
dwStart = GetTickCount();
for (k = 0; k < count;++k)
{
for (i = 0; i < 256; ++i)
{
for (j = 1; j < 256; ++j)
{
tmp = atan(double(i) /double(j));
// tmp = atan(rand());
// cout << tmp << endl;
}
}
}
dwEnd = GetTickCount();
cout << "The atan run time is:" << (dwEnd - dwStart) << "ms!" << endl;//输出运行时间
dwStart = GetTickCount();
for (k = 0; k < count; ++k)
{
for (i = 0; i < 256; ++i)
{
for (j = 1; j < 256; ++j)
{
tmp = atan2(double(i) , double(j));
// tmp = atan2(rand(), j);
}
}
}
dwEnd = GetTickCount();
cout << "The atan2 run time is:" << (dwEnd - dwStart) << "ms!" << endl;//输出运行时间
dwStart = GetTickCount();
for (k = 0; k < count; ++k)
{
for (i = 0; i < 256; ++i)
{
for (j = 1; j < 256; ++j)
{
tmp = atan(double(i) / double(j));
if (j > 0)
{
tmp += 1.7;
}
// tmp = atan(rand());
// cout << tmp << endl;
}
}
}
dwEnd = GetTickCount();
cout << "The atan + if run time is:" << (dwEnd - dwStart) << "ms!" << endl;//输出运行时间
dwStart = GetTickCount();
for (k = 0; k < count; ++k)
{
for (i = 0; i < 256; ++i)
{
for (j = 1; j < 256; ++j)
{
tmp = asin(double(i) / double(j));
// tmp = asin(rand() / RAND_MAX);
}
}
}
dwEnd = GetTickCount();
cout << "The asin run time is:" << (dwEnd - dwStart) << "ms!" << endl;//输出运行时间
dwStart = GetTickCount();
for (k = 0; k < count; ++k)
{
for (i = 0; i < 256; ++i)
{
for (j = 1; j < 256; ++j)
{
tmp = asin(double(i) / double(j));
// tmp = acos(rand() / RAND_MAX);
}
}
}
dwEnd = GetTickCount();
cout << "The acos run time is:" << (dwEnd - dwStart) << "ms!" << endl;//输出运行时间
dwStart = GetTickCount();
for (k = 0; k < count; ++k)
{
for (i = 0; i < 256; ++i)
{
for (j = 1; j < 256; ++j)
{
tmp = tanh(double(i) / double(j));
// tmp = tanh(rand());
}
}
}
dwEnd = GetTickCount();
cout << "The tanh run time is:" << (dwEnd - dwStart) << "ms!" << endl;//输出运行时间
dwStart = GetTickCount();
for (k = 0; k < count; ++k)
{
for (i = 0; i < 256; ++i)
{
for (j = 1; j < 256; ++j)
{
tmp = exp(double(i) / j);
}
}
}
dwEnd = GetTickCount();
cout << "The exp run time is:" << (dwEnd - dwStart) << "ms!" << endl;//输出运行时间*/
dwStart = GetTickCount();
for (k = 0; k < count; ++k)
{
for (i = 0; i < 256; ++i)
{
for (j = 1; j < 256; ++j)
{
tmp = log(double(i) / double(j));
// tmp = log(rand() / RAND_MAX);
}
}
}
dwEnd = GetTickCount();
cout << "The log run time is:" << (dwEnd - dwStart) << "ms!" << endl;//输出运行时间
dwStart = GetTickCount();
for (k = 0; k < count; ++k)
{
for (i = 0; i < 256; ++i)
{
for (j = 1; j < 256; ++j)
{
//tmp = tan(i);
tmp = tan(double(i) / double(j));
//tmp = tan(rand());
}
}
}
dwEnd = GetTickCount();
cout << "The tan run time is:" << (dwEnd - dwStart) << "ms!" << endl;//输出运行时间
dwStart = GetTickCount();
for (k = 0; k < count; ++k)
{
for (i = 0; i < 256; ++i)
{
for (j = 1; j < 256; ++j)
{
tmp = sin(double(i) / double(j));
// tmp = sin(rand() / RAND_MAX);
}
}
}
dwEnd = GetTickCount();
cout << "The sin run time is:" << (dwEnd - dwStart) << "ms!" << endl;//输出运行时间
dwStart = GetTickCount();
for (k = 0; k < count; ++k)
{
for (i = 0; i < 256; ++i)
{
for (j = 1; j < 256; ++j)
{
tmp = cos(double(i) / double(j));
// tmp = cos(rand() / RAND_MAX);
}
}
}
dwEnd = GetTickCount();
cout << "The cos run time is:" << (dwEnd - dwStart) << "ms!" << endl;//输出运行时间
dwStart = GetTickCount();
for (k = 0; k < count; ++k)
{
for (i = 0; i < 256; ++i)
{
for (j = 1; j < 256; ++j)
{
tmp = double(i) / double(j);
// tmp = double(i) /(rand() / RAND_MAX);
}
}
}
dwEnd = GetTickCount();
cout << "The div run time is:" << (dwEnd - dwStart) << "ms!" << endl;//输出运行时间
dwStart = GetTickCount();
for (k = 0; k < count; ++k)
{
for (i = 0; i < 256; ++i)
{
for (j = 1; j < 256; ++j)
{
tmp = double(i) * double(j);// (rand() / RAND_MAX);
}
}
}
dwEnd = GetTickCount();
cout << "The mult run time is:" << (dwEnd - dwStart) << "ms!" << endl;//输出运行时间
dwStart = GetTickCount();
for (k = 0; k < count; ++k)
{
for (i = 0; i < 256; ++i)
{
for (j = 1; j < 256; ++j)
{
tmp = double(i) + double(j);// (rand() / RAND_MAX);
}
}
}
dwEnd = GetTickCount();
cout << "The add run time is:" << (dwEnd - dwStart) << "ms!" << endl;//输出运行时间
dwStart = GetTickCount();
for (k = 0; k < count; ++k)
{
for (i = 0; i < 256; ++i)
{
for (j = 1; j < 256; ++j)
{
tmp = double(i) - double(j);// / RAND_MAX;
}
}
}
dwEnd = GetTickCount();
cout << "The add run time is:" << (dwEnd - dwStart) << "ms!" << endl;//输出运行时间
clock_t end_time = clock();
dwEnd = GetTickCount();
time = double(end_time - start_time) / CLOCKS_PER_SEC;
cout << time << "s" << endl;
cout << "The run time is:" << (dwEnd - dwStart) << "ms!" << endl;//输出运行时间
cin >> i;
return 0;
}