Try-catch的代价

#include<iostream>
#include<time.h>
#include<windows.h>

#define nullptr NULL
#define TEST 10000000
void test_noTry(unsigned count_){

	volatile unsigned count = count_;
	while(count--)
		double dRet = 3.1415926*3.1415927;
}
void test_Try(unsigned count_){

	volatile unsigned count = count_;	
	try{
		while(count--)
			double dRet = 3.1415926*3.1415927;
			throw std::exception();
			
	}
	catch(...)
	{
		//do nothing...
	}
}

void run(void (*f)(unsigned) , unsigned count)
{
	for(int i = 0; i<TEST; i++)
		f(count);
}

int main()
{
	FILETIME ft1 = { 0 }, ft2 = { 0 }, ft3 = { 0 };
	unsigned count = 100000000;//涓€浜?
	GetSystemTimeAsFileTime(&ft1);
	run(test_noTry, 10);
	GetSystemTimeAsFileTime(&ft2);
	run(test_Try, 10);
	GetSystemTimeAsFileTime(&ft3);
	std::cout<<"dt1 = "<<ft2.dwHighDateTime - ft1.dwHighDateTime<<std::endl;
	std::cout<<"dt2 = "<<ft3.dwHighDateTime - ft2.dwHighDateTime<<std::endl;
	std::cout<<"dt1 = "<<ft2.dwLowDateTime - ft1.dwLowDateTime<<std::endl;
	std::cout<<"dt2 = "<<ft3.dwLowDateTime - ft2.dwLowDateTime<<std::endl;
	std::cin.ignore();
	return 0;
}
GetSystemTimeAsFileTime

https://docs.microsoft.com/zh-cn/windows/desktop/api/sysinfoapi/nf-sysinfoapi-getsystemtimeasfiletime
try-catch
标题

上述只是简单的结果测试,证明try-catch的代价还是挺大的。

-> next article Try-catch原理浅析

猜你喜欢

转载自blog.csdn.net/qq_36908710/article/details/84488550