Click here to Skip to main content
15,896,201 members
Articles / Programming Languages / C++

CProfile - A Simple Class to Do Code Profiling and Tracing

Rate me:
Please Sign up or sign in to vote.
4.33/5 (10 votes)
1 May 2003CPOL4 min read 65.9K   700   25  
This simple class allows to profile code sections and optionally print time elapsed and tracing strings in different formats.
// IMPLEMENTATION FOR THE CProfiler CLASS
// (c) Hernan Di Pietro 2003.
/////////////////////////////////////////////////////////////////
#include <cstdio>
#include "profiler.h"

CProfiler::CProfiler(void)
{
	// constructor zeroes all LARGE_INTEGER data
	// members and gets the correct counter frequency
	// for the system.
	ZeroMemory(&m_QPFrequency, sizeof(m_QPFrequency));
	ZeroMemory(&m_ElapsedTime, sizeof(m_ElapsedTime));
	ZeroMemory(&m_StartCounter,sizeof(m_StartCounter));
	m_Retval = 0;

	// Query Frecuency
	m_Retval = QueryPerformanceFrequency(&m_QPFrequency);	
}

CProfiler::~CProfiler(void)
{
	// destructor does nothing specific...
}

/////////////////////////////////////////////////////////////
// GetLastRetVal()
// returns error code for the latest API function called
/////////////////////////////////////////////////////////////
inline DWORD CProfiler::GetLastRetVal()
{
	return static_cast<DWORD>(m_Retval);
}

/////////////////////////////////////////////////////////////
// ProfileStart
// Starts time count, specifying the debug log type
// which is written when ProfileEnd is called.
// May be LOGNONE (default), LOGTICKS, LOGSECS, LOGALL
////////////////////////////////////////////////////////////
void CProfiler::ProfileStart(LOGTYPE logtype)
{
	// get and store start time
	m_Retval = QueryPerformanceCounter (&m_StartCounter);
	// store logging type
	m_LogType = logtype;
}

/////////////////////////////////////////////////////////////
// ProfileEnd 
// End profiling and optionally write a string to the
// debug window or to a message box, depending on the
// LOGTYPE specified on ProfileStart(...)
////////////////////////////////////////////////////////////
__int64 CProfiler::ProfileEnd(char* TraceStr)
{
	// get and store finishing time and calc elapsed time(ticks)
	m_Retval = QueryPerformanceCounter (&m_EndCounter);
	m_ElapsedTime = (m_EndCounter.QuadPart  - m_StartCounter.QuadPart );

    // output debugging log?
    if (m_LogType != LOGNONE)
	{
		// variables for output
		char* MsgOut;
		char tmpbuf[300];        
		
		if (m_LogType == LOGTICKS)	// output in ticks
            sprintf(tmpbuf, "** ProfileEnd: %I64d clock ticks elapsed.\n", m_ElapsedTime);

		if (m_LogType == LOGSECS)  // output in secs		
			sprintf(tmpbuf, "** ProfileEnd: %.3fsecs. elapsed.\n", SecsFromTicks(m_ElapsedTime)); 		

		if (m_LogType == LOGMSGBOX)
			sprintf(tmpbuf, "\n\n%I64d clock ticks (%.3fsecs.) elapsed.\n", 
					m_ElapsedTime, SecsFromTicks(m_ElapsedTime));

		if (m_LogType == LOGALL)
			sprintf(tmpbuf, "** Profile End **\n[CPU Hires counter freq is: %I64d clock ticks" 
			                "per second.\n%I64d clock ticks (%.3fsecs.) elapsed.\n", 
							m_QPFrequency.QuadPart ,m_ElapsedTime, SecsFromTicks(m_ElapsedTime)); 

		MsgOut = new char[strlen(tmpbuf)+strlen(TraceStr)+1];
		strcpy (MsgOut, TraceStr);
		strcat (MsgOut, tmpbuf);

		// select output to msgbox or debug-results window
		if (m_LogType == LOGMSGBOX)
		{
            MessageBox (NULL, MsgOut, "CProfiler::ProfileEnd Timing", 
						MB_ICONINFORMATION | MB_OK);
		}
		else
		{
			OutputDebugString(MsgOut);
		}

		delete [] MsgOut;
	}
	return m_ElapsedTime;
}

// The following function convert ticks to seconds
double CProfiler::SecsFromTicks (__int64 ticks)
{
	return static_cast<double>(ticks) / static_cast<double>(m_QPFrequency.QuadPart);
}

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 Code Project Open License (CPOL)


Written By
Software Developer Nektra S.A
Argentina Argentina
Started the programming journey with the venerable Commodore 64, experimenting with 6510 ASM,BASIC and tapes. In 1990 I began playing with QuickBASIC, MSDOS, Win3.0 in a NECV20 XT clone, floppy-only system. With my first 386 system, I studied Turbo Pascal and C, and began exploring VGA graphics. Discovered Visual Basic under Windows 3.1. Moved to full C and C++ development in the 2000s, after Microsoft moved to .NET.

Comments and Discussions