comparison.cpp

来自「为大家搜集免费的计算机学习、编程资料和优秀的网络资源。多多支持」· C++ 代码 · 共 61 行

CPP
61
字号
#include "Dice.h"
#include <iostream>

using std::wcout;
using std::endl;

#define for if(0);else for



int main()
{
	Dice dice(10);
	unsigned long diceTotal = 0;
	time_t beg, end;

	beg = time(NULL);
	diceTotal = dice.multi(500000000);
	end = time(NULL);

	wcout << L"Using member function multi(), total = " << diceTotal << endl;
	wcout << L"  in " << (end - beg) << L" seconds." << endl << endl;

	diceTotal = 0;

	beg = time(NULL);
	for(long i=0; i<500000000; ++i)
	{
		diceTotal += dice.roll();
	}
	end = time(NULL);

	wcout << L"Using member function roll(), total = " << diceTotal << endl;
	wcout << L"  in " << (end - beg) << L" seconds." << endl << endl;

	diceTotal = 0;

	beg = time(NULL);
	for(long i=0; i<500000000; ++i)
	{
		diceTotal += Dice(10);
	}
	end = time(NULL);

	wcout << L"Repeatedly constructing Dice objects, total = " << diceTotal << endl;
	wcout << L"  in " << (end - beg) << L" seconds." << endl << endl;

	diceTotal = 0;

	beg = time(NULL);
	for(long i=0; i<500000000; ++i)
	{
		diceTotal += static_cast<int>(rand()%(10)) + 1;
	}
	end = time(NULL);

	wcout << L"Directly calling rand(), total = " << diceTotal << endl;
	wcout << L"  in " << (end - beg) << L" seconds." << endl << endl;

	return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?