Basic Profiler
Download the files here:
|
|
|
//START OF HEADER FILE (INTERFACE)
//----------------------------------------------------------
#include "windows.h"
#include <vector>
#include <string>
#include <fstream>
#pragma once
#define MILLION 1000000
#define THOUSAND 1000
class RecordKeeper
{
public:
RecordKeeper(std::string name);
~RecordKeeper();
void AddElapsedTime(LONGLONG additionalTime);
private:
static std::ofstream OutputFile;
std::string Name;
LONGLONG ElapsedTime;
LONGLONG recordCount;
};
class Profiler
{
public:
Profiler(RecordKeeper* record);
~Profiler();
private:
LARGE_INTEGER StartTime;
RecordKeeper* myRecord;
};
//macro for the user to call to use the profiler
//makes static so that at the end of the program everthing is gathered and destroyed
#define RecordFunction static RecordKeeper FunctionRecord(__FUNCTION__); \
Profiler Timer(&FunctionRecord)
//----------------------------------------------------------
//END OF HEADER FILE
//START OF SOURCE FILE (IMPLEMENTATION)
//----------------------------------------------------------
std::ofstream RecordKeeper::OutputFile("ProfileResults.txt");
//sets defaults but main used to save the name of the function we are recording
RecordKeeper::RecordKeeper(std::string name) : Name(name), ElapsedTime(0), recordCount(0)
{
}
//prints out profiled information on file
RecordKeeper::~RecordKeeper()
{
//variable to save the frequency that Query preforms at
LARGE_INTEGER frequency;
QueryPerformanceFrequency(&frequency);
//multiple to get our recorded time within the MicroSeconds range
ElapsedTime *= MILLION;
//devide by the frequency to get the actual time value
ElapsedTime /= frequency.QuadPart;
//find the average time the function runs by deviding the total time by how many times the function was called
LONGLONG AverageTime = ElapsedTime / recordCount;
//print the name of our function
OutputFile << Name << std::endl;
//display the average time in microseconds
OutputFile << " AverageTime: " << (AverageTime / MILLION) << " Seconds " << ((AverageTime / THOUSAND) % 999) << " Miliseconds and " << (AverageTime % 999) << "MicroSeconds" <<std::endl;
//display the total time in microseconds
OutputFile << " TotalTime: " << (ElapsedTime / MILLION) << " Seconds " << ((ElapsedTime / THOUSAND) % 999) << " Miliseconds and " << (ElapsedTime % 999) << "MicroSeconds" <<std::endl << std::endl;
}
void RecordKeeper::AddElapsedTime(LONGLONG additionalTime)
{
//add to the total recorded time
ElapsedTime += additionalTime;
//increase the amount this function has been called
recordCount++;
}
Profiler::Profiler(RecordKeeper* record) : myRecord(record)
{
//start our timer
QueryPerformanceCounter(&StartTime);
}
Profiler::~Profiler()
{
//variable to record our end time
LARGE_INTEGER EndTime;
//save what time it is now
QueryPerformanceCounter(&EndTime);
//add the recorded time to the functions total time
myRecord->AddElapsedTime(EndTime.QuadPart - StartTime.QuadPart);
}
//----------------------------------------------------------
//END OF SOURCE FILE
//----------------------------------------------------------
#include "windows.h"
#include <vector>
#include <string>
#include <fstream>
#pragma once
#define MILLION 1000000
#define THOUSAND 1000
class RecordKeeper
{
public:
RecordKeeper(std::string name);
~RecordKeeper();
void AddElapsedTime(LONGLONG additionalTime);
private:
static std::ofstream OutputFile;
std::string Name;
LONGLONG ElapsedTime;
LONGLONG recordCount;
};
class Profiler
{
public:
Profiler(RecordKeeper* record);
~Profiler();
private:
LARGE_INTEGER StartTime;
RecordKeeper* myRecord;
};
//macro for the user to call to use the profiler
//makes static so that at the end of the program everthing is gathered and destroyed
#define RecordFunction static RecordKeeper FunctionRecord(__FUNCTION__); \
Profiler Timer(&FunctionRecord)
//----------------------------------------------------------
//END OF HEADER FILE
//START OF SOURCE FILE (IMPLEMENTATION)
//----------------------------------------------------------
std::ofstream RecordKeeper::OutputFile("ProfileResults.txt");
//sets defaults but main used to save the name of the function we are recording
RecordKeeper::RecordKeeper(std::string name) : Name(name), ElapsedTime(0), recordCount(0)
{
}
//prints out profiled information on file
RecordKeeper::~RecordKeeper()
{
//variable to save the frequency that Query preforms at
LARGE_INTEGER frequency;
QueryPerformanceFrequency(&frequency);
//multiple to get our recorded time within the MicroSeconds range
ElapsedTime *= MILLION;
//devide by the frequency to get the actual time value
ElapsedTime /= frequency.QuadPart;
//find the average time the function runs by deviding the total time by how many times the function was called
LONGLONG AverageTime = ElapsedTime / recordCount;
//print the name of our function
OutputFile << Name << std::endl;
//display the average time in microseconds
OutputFile << " AverageTime: " << (AverageTime / MILLION) << " Seconds " << ((AverageTime / THOUSAND) % 999) << " Miliseconds and " << (AverageTime % 999) << "MicroSeconds" <<std::endl;
//display the total time in microseconds
OutputFile << " TotalTime: " << (ElapsedTime / MILLION) << " Seconds " << ((ElapsedTime / THOUSAND) % 999) << " Miliseconds and " << (ElapsedTime % 999) << "MicroSeconds" <<std::endl << std::endl;
}
void RecordKeeper::AddElapsedTime(LONGLONG additionalTime)
{
//add to the total recorded time
ElapsedTime += additionalTime;
//increase the amount this function has been called
recordCount++;
}
Profiler::Profiler(RecordKeeper* record) : myRecord(record)
{
//start our timer
QueryPerformanceCounter(&StartTime);
}
Profiler::~Profiler()
{
//variable to record our end time
LARGE_INTEGER EndTime;
//save what time it is now
QueryPerformanceCounter(&EndTime);
//add the recorded time to the functions total time
myRecord->AddElapsedTime(EndTime.QuadPart - StartTime.QuadPart);
}
//----------------------------------------------------------
//END OF SOURCE FILE