Sunday, June 10, 2012

time your code on windows

just a few notes about how to time your code ...

the following code assumes we already include the following header files:

#include "stdafx.h"
#include
#include
#include
#include
#include


method 1:

    __int64 ctr1 = 0, ctr2 = 0, freq = 0;
    QueryPerformanceCounter((LARGE_INTEGER *)&ctr1);
    
// do something here

    QueryPerformanceCounter((LARGE_INTEGER *)&ctr2);
    QueryPerformanceFrequency((LARGE_INTEGER *)&freq);
    printf("QueryPerformanceCounter minimum resolution: 1/%f Seconds.\n", freq);
    printf("100 Increment time: %f seconds.\n",((ctr2 - ctr1) * 1.0 / freq));



method 2:

typedef DWORD (WINAPI * GetTickCount_t)(void);
HANDLE p1 = GetCurrentProcess();
HMODULE hKernel = LoadLibrary(L"kernel32.dll");
GetTickCount_t pfnGetTickCount = (GetTickCount_t)GetProcAddress(hKernel, "GetTickCount");

 DWORD tick1 = pfnGetTickCount(); 
// do something here
DWORD tick2 = pfnGetTickCount();
printf("when using GetTickCount: %d milliseconds.\n",(tick2-tick1));


 

0 Comments:

Post a Comment

<< Home