三目运算符?:与If效率对比

#include "stdafx.h"
#include 
#include 
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	int a(100), b(50), n(100000), temp; 
	clock_t start = clock();
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= n; j++)
		{
			if (a > b)
				temp = a;
			else
				temp = b;
		}
	}
	clock_t end = clock();
	cout << "IF Time = " << end - start << endl;
	start = clock();
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= n; j++)
		{
			temp = a > b ? a : b;
		}
	}
	end = clock();
	cout << "?: Time = " << end - start << endl;
	return 0;
}
三目运算符?:与If效率对比_第1张图片

你可能感兴趣的:(三目运算符)