Click here to Skip to main content
15,892,059 members
Articles / Programming Languages / C++

A C++ class for more precise time interval measurement

Rate me:
Please Sign up or sign in to vote.
4.66/5 (28 votes)
17 Oct 2001Ms-PL2 min read 169K   2.7K   60  
A C++ class capable to measure time intervals in microseconds under Windows OS
#include "stdafx.h"
#include "PreciseTimer.h"

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

string Int64ToString(__int64 const& ri64, int iRadix=10)
{
	bool bNeg = (ri64 < 0);
	__int64 i64 = ri64;
	string ostrRes;
	bool bSpecial = false;
	if(true == bNeg)
	{
		i64 = -i64;
		if(i64 < 0)
			//Special case number -9223372036854775808 or 0x8000000000000000
			bSpecial = true;
		ostrRes.append(1, '-');
	}
	int iR;
	do
	{
		iR = i64 % iRadix;
		if(true == bSpecial)
			iR = -iR;
		if(iR < 10)
			ostrRes.append(1, '0' + iR);
		else
			ostrRes.append(1, 'A' + iR - 10);
		i64 /= iRadix;
	}
	while(i64 != 0);
	//Reverse the string
	string::iterator it = ostrRes.begin();
	if(bNeg)
		it++;
	reverse(it, ostrRes.end());
	return ostrRes;
}

int main()
{
	CPreciseTimer oPreciseTimer;
	cout << "Starting!" << endl;
	oPreciseTimer.StartTimer();
	DWORD dwStart = ::GetTickCount();
	::Sleep(5000);
	cout << "Ending!" << endl;
	oPreciseTimer.StopTimer();
	DWORD dwDiff = (::GetTickCount() - dwStart)*1000;
	__int64 i64Diff = oPreciseTimer.GetTime();
	cout << "Diff1 (GetTickCount()) = " << dwDiff << endl;
	cout << "Diff2 (PreciseTimer) = " << Int64ToString(i64Diff) << endl;
	return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Web Developer
Romania Romania
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions