I use a class for this, its designed to measure the time taken to execute a function and write that to a uth-16le text file (I need to update this to use a new class i made for this... but nm).
Create a new instance at the top of a function, e.g. jProfiler(L"myFunction") and the cleanup at the end of the function will do the rest, if you want to be sure though new and delete it yourself. Its a bit overkill for a small test, but might help:
// start header
/* jProfiler class by Semi Essessi
*
* (class description goes here)
*
*/
#ifndef __JPROFILER_H
#define __JPROFILER_H
#include <stdio.h>
#include <windows.h>
class jProfiler
{
private:
wchar_t* str;
LARGE_INTEGER start;
LARGE_INTEGER tps;
LARGE_INTEGER buf;
static FILE* f;
static int instCount;
static void Initialise();
static void Shutdown();
public:
jProfiler(const wchar_t* msg);
~jProfiler();
};
#endif
// - end header
/* jProfiler class by Semi Essessi
*
* (class description goes here)
*
*/
#include "jProfiler.h"
#include <windows.h>
FILE* jProfiler::f = 0;
int jProfiler::instCount = 0;
jProfiler::jProfiler(const wchar_t* msg)
{
// constructor code for menuVar
int i = (int)wcslen(msg)+1;
str = new wchar_t[i];
memcpy(str, msg, sizeof(wchar_t)*i);
str[i-1] = 0;
QueryPerformanceFrequency(&tps);
QueryPerformanceCounter(&start);
instCount++;
Initialise();
}
jProfiler::~jProfiler()
{
// destructor code for menuVar
QueryPerformanceCounter(&buf);
// work out change in time
double dt=((float)buf.QuadPart - (float)start.QuadPart)/(float)tps.QuadPart;
fwprintf(f, L"%s : %.20f\r\n", str, dt);
if(str) delete[] str;
instCount--;
Shutdown();
}
void jProfiler::Initialise()
{
if(!f)
{
f = _wfopen(L"profilerlog.txt", L"wb");
unsigned short a = 0xFEFF;
fwrite(&a, sizeof(unsigned short), 1, f);
}
}
void jProfiler::Shutdown()
{
if(instCount==0) if(f) fclose(f);
}