Replace QPC to RDTSC Timer in games

Everything about latency. This section is mainly user/consumer discussion. (Peer-reviewed scientific discussion should go in Laboratory section). Tips, mouse lag, display lag, game engine lag, network lag, whole input lag chain, VSYNC OFF vs VSYNC ON, and more! Input Lag Articles on Blur Busters.
Kaldaien
Posts: 21
Joined: 22 Jan 2020, 21:27

Re: Replace QPC to RDTSC Timer in games

Post by Kaldaien » 15 Aug 2021, 14:25

I'm not convinced this is a good idea :)

The reason we moved away from using RDTSC for timing is because 1) CPUs have dynamic frequency scaling and 2) each CPU core has a different RDTSC timestamp.

QPC is monotonic and the frequency is constant no matter what CPU core any timing code happens to be running on.

pox02
Posts: 262
Joined: 28 Sep 2018, 06:04

Re: Replace QPC to RDTSC Timer in games

Post by pox02 » 26 Dec 2021, 11:47

i would like to update code so u guys get more game feeling by using the function QueryThreadCycleTime enjoy :D

Code: Select all

LONGLONG GetFrequency(DWORD sleepTime)
{
    LARGE_INTEGER fq, st, ed;
    QueryPerformanceFrequency(&fq);
    QueryPerformanceCounter(&st);
    QueryThreadCycleTime(GetCurrentThread(), &st);
    LONGLONG begin = GetCycleCount();
    Sleep(sleepTime);
    QueryPerformanceCounter(&ed);
    QueryThreadCycleTime(GetCurrentThread(), &ed);
    LONGLONG end = GetCycleCount();
    return (end - begin) * fq.QuadPart / (ed.QuadPart - st.QuadPart);
}

monitors xg258q aw2518hf 27GK750F-B pg248q xg240r lg w2363d-pf xb270hu XL2546 XL2546K NXG252R

Bobo
Posts: 112
Joined: 05 Jun 2018, 11:44

Re: Replace QPC to RDTSC Timer in games

Post by Bobo » 26 Dec 2021, 18:47

what exactly is this and how do you use it ?

Aspector
Posts: 1
Joined: 27 Dec 2021, 12:09

Re: Replace QPC to RDTSC Timer in games

Post by Aspector » 27 Dec 2021, 12:12

pox02 wrote:
26 Dec 2021, 11:47
i would like to update code so u guys get more game feeling by using the function QueryThreadCycleTime enjoy :D

Code: Select all

LONGLONG GetFrequency(DWORD sleepTime)
{
    LARGE_INTEGER fq, st, ed;
    QueryPerformanceFrequency(&fq);
    QueryPerformanceCounter(&st);
    QueryThreadCycleTime(GetCurrentThread(), &st);
    LONGLONG begin = GetCycleCount();
    Sleep(sleepTime);
    QueryPerformanceCounter(&ed);
    QueryThreadCycleTime(GetCurrentThread(), &ed);
    LONGLONG end = GetCycleCount();
    return (end - begin) * fq.QuadPart / (ed.QuadPart - st.QuadPart);
}

And how do we use it or compile it?

AndreyRGW
Posts: 12
Joined: 01 Aug 2021, 11:12

Re: Replace QPC to RDTSC Timer in games

Post by AndreyRGW » 27 Dec 2021, 13:29

pox02 wrote:
26 Dec 2021, 11:47
i would like to update code so u guys get more game feeling by using the function QueryThreadCycleTime enjoy :D

Code: Select all

LONGLONG GetFrequency(DWORD sleepTime)
{
    LARGE_INTEGER fq, st, ed;
    QueryPerformanceFrequency(&fq);
    QueryPerformanceCounter(&st);
    QueryThreadCycleTime(GetCurrentThread(), &st);
    LONGLONG begin = GetCycleCount();
    Sleep(sleepTime);
    QueryPerformanceCounter(&ed);
    QueryThreadCycleTime(GetCurrentThread(), &ed);
    LONGLONG end = GetCycleCount();
    return (end - begin) * fq.QuadPart / (ed.QuadPart - st.QuadPart);
}

I compiled RDTSC with your code, but I didn't notice any changes compared to the original.

pox02
Posts: 262
Joined: 28 Sep 2018, 06:04

Re: Replace QPC to RDTSC Timer in games

Post by pox02 » 27 Dec 2021, 14:48

AndreyRGW wrote:
27 Dec 2021, 13:29
pox02 wrote:
26 Dec 2021, 11:47
i would like to update code so u guys get more game feeling by using the function QueryThreadCycleTime enjoy :D

Code: Select all

LONGLONG GetFrequency(DWORD sleepTime)
{
    LARGE_INTEGER fq, st, ed;
    QueryPerformanceFrequency(&fq);
    QueryPerformanceCounter(&st);
    QueryThreadCycleTime(GetCurrentThread(), &st);
    LONGLONG begin = GetCycleCount();
    Sleep(sleepTime);
    QueryPerformanceCounter(&ed);
    QueryThreadCycleTime(GetCurrentThread(), &ed);
    LONGLONG end = GetCycleCount();
    return (end - begin) * fq.QuadPart / (ed.QuadPart - st.QuadPart);
}

I compiled RDTSC with your code, but I didn't notice any changes compared to the original.
Try add SetProcessAffinityMask in first core see if any improvement

Code: Select all

SetProcessAffinityMask(GetCurrentProcess(), 1);
test fix overhead of QueryPerformanceCounter didnt try for long time so give any updates of it

Code: Select all

 LARGE_INTEGER fq, st, ed;
    QueryPerformanceFrequency(&fq);
    QueryPerformanceCounter(&st);
    LONGLONG begin = GetCycleCount();
    Sleep(sleepTime);
    QueryPerformanceCounter(&ed);
    1000000 * (ed.QuadPart - st.QuadPart), fq;
    LONGLONG end = GetCycleCount();
    return (end - begin) * fq.QuadPart / (ed.QuadPart - st.QuadPart);
    
monitors xg258q aw2518hf 27GK750F-B pg248q xg240r lg w2363d-pf xb270hu XL2546 XL2546K NXG252R

AndreyRGW
Posts: 12
Joined: 01 Aug 2021, 11:12

Re: Replace QPC to RDTSC Timer in games

Post by AndreyRGW » 28 Dec 2021, 08:54

pox02 wrote:
27 Dec 2021, 14:48
AndreyRGW wrote:
27 Dec 2021, 13:29
pox02 wrote:
26 Dec 2021, 11:47
i would like to update code so u guys get more game feeling by using the function QueryThreadCycleTime enjoy :D

Code: Select all

LONGLONG GetFrequency(DWORD sleepTime)
{
    LARGE_INTEGER fq, st, ed;
    QueryPerformanceFrequency(&fq);
    QueryPerformanceCounter(&st);
    QueryThreadCycleTime(GetCurrentThread(), &st);
    LONGLONG begin = GetCycleCount();
    Sleep(sleepTime);
    QueryPerformanceCounter(&ed);
    QueryThreadCycleTime(GetCurrentThread(), &ed);
    LONGLONG end = GetCycleCount();
    return (end - begin) * fq.QuadPart / (ed.QuadPart - st.QuadPart);
}

I compiled RDTSC with your code, but I didn't notice any changes compared to the original.
Try add SetProcessAffinityMask in first core see if any improvement

Code: Select all

SetProcessAffinityMask(GetCurrentProcess(), 1);
test fix overhead of QueryPerformanceCounter didnt try for long time so give any updates of it

Code: Select all

 LARGE_INTEGER fq, st, ed;
    QueryPerformanceFrequency(&fq);
    QueryPerformanceCounter(&st);
    LONGLONG begin = GetCycleCount();
    Sleep(sleepTime);
    QueryPerformanceCounter(&ed);
    1000000 * (ed.QuadPart - st.QuadPart), fq;
    LONGLONG end = GetCycleCount();
    return (end - begin) * fq.QuadPart / (ed.QuadPart - st.QuadPart);
    
I think it only got worse after that.

pox02
Posts: 262
Joined: 28 Sep 2018, 06:04

Re: Replace QPC to RDTSC Timer in games

Post by pox02 » 31 Dec 2021, 23:53

AndreyRGW wrote:
28 Dec 2021, 08:54
pox02 wrote:
27 Dec 2021, 14:48
AndreyRGW wrote:
27 Dec 2021, 13:29
pox02 wrote:
26 Dec 2021, 11:47
i would like to update code so u guys get more game feeling by using the function QueryThreadCycleTime enjoy :D

Code: Select all

LONGLONG GetFrequency(DWORD sleepTime)
{
    LARGE_INTEGER fq, st, ed;
    QueryPerformanceFrequency(&fq);
    QueryPerformanceCounter(&st);
    QueryThreadCycleTime(GetCurrentThread(), &st);
    LONGLONG begin = GetCycleCount();
    Sleep(sleepTime);
    QueryPerformanceCounter(&ed);
    QueryThreadCycleTime(GetCurrentThread(), &ed);
    LONGLONG end = GetCycleCount();
    return (end - begin) * fq.QuadPart / (ed.QuadPart - st.QuadPart);
}

I compiled RDTSC with your code, but I didn't notice any changes compared to the original.
Try add SetProcessAffinityMask in first core see if any improvement

Code: Select all

SetProcessAffinityMask(GetCurrentProcess(), 1);
test fix overhead of QueryPerformanceCounter didnt try for long time so give any updates of it

Code: Select all

 LARGE_INTEGER fq, st, ed;
    QueryPerformanceFrequency(&fq);
    QueryPerformanceCounter(&st);
    LONGLONG begin = GetCycleCount();
    Sleep(sleepTime);
    QueryPerformanceCounter(&ed);
    1000000 * (ed.QuadPart - st.QuadPart), fq;
    LONGLONG end = GetCycleCount();
    return (end - begin) * fq.QuadPart / (ed.QuadPart - st.QuadPart);
    
I think it only got worse after that.
i see one last attempt

Code: Select all

LONGLONG GetFrequency(DWORD sleepTime)
{
    LARGE_INTEGER fq, st, ed;
    SetPriorityClass(GetCurrentThread(), REALTIME_PRIORITY_CLASS);
    SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
    SetProcessPriorityBoost(GetCurrentThread(), 0);
    SetThreadAffinityMask(GetCurrentThread(), 0x1);
    SetProcessAffinityMask(GetCurrentProcess(), 0x0);
    QueryPerformanceFrequency(&fq);
    QueryPerformanceCounter(&st);
    LONGLONG begin = GetCycleCount();
	Sleep(sleepTime);
    QueryPerformanceCounter(&ed);
    LONGLONG end = GetCycleCount();
    return (end - begin) * fq.QuadPart / (ed.QuadPart - st.QuadPart);
}
monitors xg258q aw2518hf 27GK750F-B pg248q xg240r lg w2363d-pf xb270hu XL2546 XL2546K NXG252R

AndreyRGW
Posts: 12
Joined: 01 Aug 2021, 11:12

Re: Replace QPC to RDTSC Timer in games

Post by AndreyRGW » 01 Jan 2022, 15:49

pox02 wrote:
31 Dec 2021, 23:53
AndreyRGW wrote:
28 Dec 2021, 08:54
pox02 wrote:
27 Dec 2021, 14:48
AndreyRGW wrote:
27 Dec 2021, 13:29


I compiled RDTSC with your code, but I didn't notice any changes compared to the original.
Try add SetProcessAffinityMask in first core see if any improvement

Code: Select all

SetProcessAffinityMask(GetCurrentProcess(), 1);
test fix overhead of QueryPerformanceCounter didnt try for long time so give any updates of it

Code: Select all

 LARGE_INTEGER fq, st, ed;
    QueryPerformanceFrequency(&fq);
    QueryPerformanceCounter(&st);
    LONGLONG begin = GetCycleCount();
    Sleep(sleepTime);
    QueryPerformanceCounter(&ed);
    1000000 * (ed.QuadPart - st.QuadPart), fq;
    LONGLONG end = GetCycleCount();
    return (end - begin) * fq.QuadPart / (ed.QuadPart - st.QuadPart);
    
I think it only got worse after that.
i see one last attempt

Code: Select all

LONGLONG GetFrequency(DWORD sleepTime)
{
    LARGE_INTEGER fq, st, ed;
    SetPriorityClass(GetCurrentThread(), REALTIME_PRIORITY_CLASS);
    SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
    SetProcessPriorityBoost(GetCurrentThread(), 0);
    SetThreadAffinityMask(GetCurrentThread(), 0x1);
    SetProcessAffinityMask(GetCurrentProcess(), 0x0);
    QueryPerformanceFrequency(&fq);
    QueryPerformanceCounter(&st);
    LONGLONG begin = GetCycleCount();
	Sleep(sleepTime);
    QueryPerformanceCounter(&ed);
    LONGLONG end = GetCycleCount();
    return (end - begin) * fq.QuadPart / (ed.QuadPart - st.QuadPart);
}
This version seems to work fine. Happy new year, by the way.

pox02
Posts: 262
Joined: 28 Sep 2018, 06:04

Re: Replace QPC to RDTSC Timer in games

Post by pox02 » 02 Jan 2022, 00:54

wrong
Last edited by pox02 on 09 Jan 2022, 09:48, edited 1 time in total.
monitors xg258q aw2518hf 27GK750F-B pg248q xg240r lg w2363d-pf xb270hu XL2546 XL2546K NXG252R

Post Reply