Replace timeGetTime() with QueryPerformanceCounter()

Update ArchHooks_Win32Static.cpp

This code provided a 7x accuracy improvement over timeGetTime(). (0.2ms vs 1.4ms std dev*3)

Rename variables to fit with existing naming mechanism
This commit is contained in:
sukibaby
2024-05-07 17:57:47 -07:00
committed by teejusb
parent 8d24f61214
commit f7a7252dbb
+15 -10
View File
@@ -9,38 +9,43 @@
#include <cstdint> #include <cstdint>
#include <vector> #include <vector>
// for timeGetTime // for QueryPerformanceCounter
#include <windows.h> #include <windows.h>
#include <mmsystem.h> #include <mmsystem.h>
#if defined(_MSC_VER) #if defined(_MSC_VER)
#pragma comment(lib, "winmm.lib") #pragma comment(lib, "winmm.lib")
#endif #endif
static bool g_bTimerInitialized; static bool g_bTimerInitialized;
/* QueryPerformanceCounter variables below.
* QueryPerformanceCounter and QueryPerformanceFrequency expect
* a LARGE_INTEGER, which is a union. These functions store data
* in the QuadPart of the LARGE_INTEGER, which is a 64-bit integer. */
LARGE_INTEGER g_liFrequency, g_liStartTime, g_liCurrentTime;
static void InitTimer() static void InitTimer()
{ {
if( g_bTimerInitialized ) if( g_bTimerInitialized )
return; return;
g_bTimerInitialized = true; g_bTimerInitialized = true;
timeBeginPeriod( 1 ); // Grab important information for QPC during the timer initialization
QueryPerformanceCounter(&g_liStartTime);
QueryPerformanceFrequency(&g_liFrequency);
} }
std::int64_t ArchHooks::GetMicrosecondsSinceStart(bool bAccurate) std::int64_t ArchHooks::GetMicrosecondsSinceStart(bool bAccurate)
{ {
// Make sure the timer is initialized
if (!g_bTimerInitialized) if (!g_bTimerInitialized)
InitTimer(); InitTimer();
std::int64_t ret = timeGetTime() * std::int64_t(1000); // Get the current time
if( bAccurate ) QueryPerformanceCounter(&g_liCurrentTime);
{
ret = FixupTimeIfLooped( ret );
ret = FixupTimeIfBackwards( ret );
}
return ret; // Calculate the elapsed time in microseconds.
return ((g_liCurrentTime.QuadPart - g_liStartTime.QuadPart) * 1000000.0) / g_liFrequency.QuadPart;
} }
static RString GetMountDir( const RString &sDirOfExecutable ) static RString GetMountDir( const RString &sDirOfExecutable )