有关 C++里一些函数的计算速度测试

有关 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;
}


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值