Skip to content

Commit

Permalink
Add testing code
Browse files Browse the repository at this point in the history
Adapted from
github.com/sawickiap/MISC/blob/master/QueryPerformanceCounterTest.cpp
  • Loading branch information
gendlin committed Jul 2, 2024
1 parent dc11d93 commit a1a5691
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 1 deletion.
116 changes: 115 additions & 1 deletion src/i_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,116 @@
#include "i_system.h"
#include "m_argv.h"
#include "version.h"
#include "i_timer.h"

#define WIN32_LEAN_AND_MEAN
#include <Windows.h>

#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>

static uint64_t count = 1000000000;

void PrintResult(const wchar_t* name, uint32_t milliseconds)
{
double nsPerCall = (double)milliseconds * 1e6 / (double)count;
uint32_t seconds = milliseconds / 1000; milliseconds %= 1000;
uint32_t minutes = seconds / 60; seconds %= 60;
uint32_t hours = minutes / 60; minutes %= 60;
wprintf(L"According to %s it took %u:%02u:%02u.%03u (%g ns per call)\n", name, hours, minutes, seconds, milliseconds, nsPerCall);
}

int qpctest()
{
LARGE_INTEGER freq;
QueryPerformanceFrequency(&freq);

//if( argc == 2 )
// swscanf_s(argv[1], L"%zu", &count);
{
wprintf(L"Executing QueryPerformanceCounter x %zu...\n", count);

LARGE_INTEGER begPerfCount;
LARGE_INTEGER lastPerfCount;
//LARGE_INTEGER sumPerfCount{}; // Unused.

uint64_t begTickCount = GetTickCount64();
for( size_t i = 0; i < count; ++i )
{
QueryPerformanceCounter(&lastPerfCount);
//sumPerfCount.QuadPart += lastPerfCount.QuadPart;
if( i == 0 )
begPerfCount.QuadPart = lastPerfCount.QuadPart;
}
uint64_t endTickCount = GetTickCount64();

PrintResult(L"GetTickCount64", (uint32_t)(endTickCount - begTickCount));

uint32_t qpcMilliseconds = (uint32_t)(uint64_t)(((lastPerfCount.QuadPart - begPerfCount.QuadPart) * 1000. / freq.QuadPart) + 0.5);
PrintResult(L"QueryPerformanceCounter", qpcMilliseconds);
}

{
wprintf(L"\nExecuting SDL_GetPerformanceCounter x %zu...\n", count);
uint64_t first_time = 0;
uint64_t last_time = 0;

uint64_t begTickCount = GetTickCount64();
for( size_t i = 0; i < count; ++i )
{
last_time = SDL_GetPerformanceCounter();
if( i == 0 )
first_time = last_time;
}
uint64_t endTickCount = GetTickCount64();

PrintResult(L"GetTickCount64", (uint32_t)(endTickCount - begTickCount));

PrintResult(L"SDL_GetPerformanceCounter", (last_time - first_time) * 1000 / SDL_GetPerformanceFrequency());
}

{
wprintf(L"\nExecuting (double) I_GetTimeUS x %zu...\n", count);
double first_time = 0.;
double last_time = 0.;

uint64_t begTickCount = GetTickCount64();
for( size_t i = 0; i < count; ++i )
{
last_time = I_GetTimeUS();
if( i == 0 )
first_time = last_time;
}
uint64_t endTickCount = GetTickCount64();

PrintResult(L"GetTickCount64", (uint32_t)(endTickCount - begTickCount));

PrintResult(L"(double) I_GetTimeUS", (last_time - first_time) * 1e-3);
}

{
wprintf(L"\nExecuting (uint64) I_GetTimeUS x %zu...\n", count);
uint64_t first_time = 0;
uint64_t last_time = 0;

uint64_t begTickCount = GetTickCount64();
for( size_t i = 0; i < count; ++i )
{
last_time = I_GetTimeUSold();
if( i == 0 )
first_time = last_time;
}
uint64_t endTickCount = GetTickCount64();

PrintResult(L"GetTickCount64", (uint32_t)(endTickCount - begTickCount));

PrintResult(L"(uint64) I_GetTimeUS", (last_time - first_time) / 1000);
}

return 0;
}


//
// D_DoomMain()
Expand Down Expand Up @@ -67,7 +177,11 @@ int main(int argc, char **argv)
exit(0);
}

D_DoomMain();
I_InitTimer();

qpctest();

//D_DoomMain();

return 0;
}
Expand Down
12 changes: 12 additions & 0 deletions src/i_timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ double I_GetTimeUS(void)
return (int64_t)SDL_GetPerformanceCounter() * baseperiod_us;
}

uint64_t I_GetTimeUSold(void)
{
uint64_t counter = SDL_GetPerformanceCounter();

if( basecounter == 0 )
{
basecounter = counter;
}

return ((counter - basecounter) * 1000000ull) / basefreq;
}

int time_scale = 100;

static uint64_t GetPerfCounter_Scaled(void)
Expand Down
2 changes: 2 additions & 0 deletions src/i_timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ int I_GetTimeMS(void);

double I_GetTimeUS(void);

uint64_t I_GetTimeUSold(void);

void I_SetTimeScale(int scale);

void I_SetFastdemoTimer(boolean on);
Expand Down

0 comments on commit a1a5691

Please sign in to comment.