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

Small C++ Code Profiler with Debug Console Window

Rate me:
Please Sign up or sign in to vote.
2.94/5 (6 votes)
9 Oct 2008CPOL7 min read 34.5K   425   25  
A small, yet powerful, code profiler with a debug console window.
//	WinMain.cpp : Basic Windows Shell.

#include <windows.h> //	Needed for Windows Applications.
#include "Profiler.h"

#define CLASSNAME "Basic Window"	//	Window Class Name.
#define WINDOWNAME "Simple Profiler Test @Samuel Batista 10/2008"	//	Window Title.

#define WINDOW_WIDTH	640					//	Window Width.
#define WINDOW_HEIGHT	480					//	Window Height.
LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);


void TestFunction();
void TestFunctionWithinAnotherFunction(int someNumber);

/***********/
/* WINMAIN */
/***********/

int WINAPI WinMain(	HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int ncmdshow)
{
	// Get a pointer to the CProfiler class Instance
	CProfiler* cprof = CProfiler::GetInstance();

	// Initialize the class and start the Debug Console
	cprof->Initialize("Profiler Test @ Samuel Batista 10/2008", LOGALL);

	// Start Profiling the entire program! YEAH! :D
	cprof->ProfileStart("WinMain", "WinMain.cpp", "See how long the program runs");

	// Prfile the creation of a basic window. Proof that the Profiler can handle profiling within profiling.
	cprof->ProfileStart("WinMain", "WinMain()", "See how long the it takes to create a basic window.");

	WNDCLASS winclass;		//	This will hold the class we create
	MSG		 msg;			//	Generic message
	HWND	 hWnd;			//	Main Window Handle.

	//	First fill in the window class stucture
	winclass.style			= CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
	winclass.lpfnWndProc	= WindowProc;
	winclass.cbClsExtra		= 0;
	winclass.cbWndExtra		= 0;
	winclass.hInstance		= hinstance;
	winclass.hIcon			= LoadIcon(NULL, IDI_APPLICATION);
	winclass.hCursor		= LoadCursor(NULL, IDC_ARROW);
	winclass.hbrBackground	= (HBRUSH)GetStockObject(BLACK_BRUSH);
	winclass.lpszMenuName	= NULL; 
	winclass.lpszClassName	= CLASSNAME;

	//	Register the window class
	if (!RegisterClass(&winclass))
		return(0);

	//	Create the window
	if (!(hWnd = CreateWindow(	CLASSNAME,											//	Class Name.
								WINDOWNAME,											//	Title of the Window.
								WS_OVERLAPPEDWINDOW | WS_VISIBLE,					//	Window Flags.
								(GetSystemMetrics(SM_CXSCREEN)>>1) - (WINDOW_WIDTH>>1),	//	Window Start Point (x, y). 
								(GetSystemMetrics(SM_CYSCREEN)>>1) - (WINDOW_HEIGHT>>1),
								WINDOW_WIDTH,										//	Width of Window.
								WINDOW_HEIGHT,										//	Height of Window.
								NULL,												//	Handle to parent.
								NULL,												//	Handle to menu.
								hinstance,											//	Application Instance.
								NULL)))												//	Creation parms.
		return(0);

	cprof->ProfileEnd("WinMain", "WinMain()");
    
	// Profile the Test Function
	cprof->ProfileStart("TestFunction", "WinMain()");
	TestFunction();
	cprof->ProfileEnd("TestFunction", "WinMain()");

	// Profile the Test Function Again, to see that it changes the number of calls for a function. In the LOG
	// file it will show the TestFunction only once, with the times it took to execute each call.
	cprof->ProfileStart("TestFunction", "WinMain()");
	TestFunction();
	cprof->ProfileEnd("TestFunction", "WinMain()");

	//	Enter main event loop
	while(1)
	{
		if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
		{ 
			//	Test if this is a quit
			if (msg.message == WM_QUIT)
				break;
			//	Translate any accelerator keys
			TranslateMessage(&msg);
			//	Send the message to the window proc
			DispatchMessage(&msg);
		}
	}

	cprof->ProfileEnd("WinMain", "WinMain.cpp");

	cprof->Shutdown();
	//	Return to Windows like this
	return (int)(msg.wParam);
}

void TestFunction()
{
	// Get a pointer to the CProfiler class Instance. By making the class a singleton you can have 
	// access to it anywhere within your program.
	CProfiler* cprof = CProfiler::GetInstance();

	for(int i = 1; i < 100000; ++i)
	{
		cprof->ProfileStart("TestFunctionWithinAnotherFunction", "TestFunction()", "", true, 10000);
		TestFunctionWithinAnotherFunction(i);
		cprof->ProfileEnd("TestFunctionWithinAnotherFunction", "TestFunction()");
	}
}

void TestFunctionWithinAnotherFunction(int someNumber)
{
	int x = someNumber;
	int y = ((someNumber * someNumber) + someNumber - someNumber*2);
	int z = ((x + y) * y) / (y+1);

	// What a weird test... 
}






#pragma region WIN32 WINDOWPROCEDURES	// This is needed for the window, does not pretain to the actual Profiler.
LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
	//	This is the main message handler of the system
	PAINTSTRUCT	ps;			//	Used in WM_PAINT
	HDC			hdc;		//	Handle to a device context

	//	What is the message 
	switch(msg)
	{
		case WM_CREATE: 
		{
			//	Do initialization stuff here
			return(0);
		}
		break;

		case WM_PAINT:
		{
			//	Start painting
			hdc = BeginPaint(hwnd,&ps);

			//	End painting
			EndPaint(hwnd,&ps);
			return(0);
		}
		break;

		case WM_DESTROY: 
		{
			//	Kill the application			
			PostQuitMessage(0);
			return(0);
		}
		break;

		default:
		break;
	}

	//	Process any messages that we didn't take care of 
	return (DefWindowProc(hwnd, msg, wparam, lparam));
}
#pragma endregion

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
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions