flat assembler
Message board for the users of flat assembler.

Index > Windows > How get nanoseconds from QueryPerformanceCounter?

Author
Thread Post new topic Reply to topic
Roman



Joined: 21 Apr 2012
Posts: 1939
Roman 11 Aug 2021, 08:12
I found this.

Code:
LARGE_INTEGER freq;
QueryPerformanceFrequency(&freq);
double nanoseconds_per_count = 1.0e9 / static_cast<double>(freq);

LARGE_INTEGER time1, time2;
QueryPerformanceCounter(&time1);
function_to_be_timed();
QueryPerformanceCounter(&time2);

int nanoseconds = (time2-time1)*nanoseconds_per_count;
    

This is correct code?
Post 11 Aug 2021, 08:12
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20628
Location: In your JS exploiting you and your system
revolution 11 Aug 2021, 08:19
Sure, the code looks about right, and will probably work fine.

But you won't get actual nano-second timing. Most systems have a much lower frequency counter that isn't anywhere near 1GHz. Each counter step is more than 1ns.

I suggest you use the QueryPerformanceFrequency function and display result to see what frequency your system has.
Post 11 Aug 2021, 08:19
View user's profile Send private message Visit poster's website Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1939
Roman 12 Aug 2021, 04:03
I get my Cpu ryzen 3500 frequency 10 000 000
I Wonder Why 10000000?
Why such a perfect number.

Why not 10603246?
Post 12 Aug 2021, 04:03
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20628
Location: In your JS exploiting you and your system
revolution 12 Aug 2021, 04:46
That sounds about right. 10MHz is quite normal for most systems. So the smallest possible resolution is 100ns.

I don't know the significance of 10603246, but you can equally ask why not <any random value>, and the answer is because that is what the system has for its timer. Somewhere on the mobo is a 10MHz clock signal feeding into a counter.
Post 12 Aug 2021, 04:46
View user's profile Send private message Visit poster's website Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1939
Roman 12 Aug 2021, 05:41
This mean 10 000 000 not 100% speed my Cpu.
Post 12 Aug 2021, 05:41
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20628
Location: In your JS exploiting you and your system
revolution 12 Aug 2021, 07:37
If you want the CPU clock then there is RDTSC.
Post 12 Aug 2021, 07:37
View user's profile Send private message Visit poster's website Reply with quote
Roman



Joined: 21 Apr 2012
Posts: 1939
Roman 14 Aug 2021, 05:50
Very funny.
I try two code and get the same time(almost 1 millisecond).
Code:
mov  rcx,2_042_000_000
            .i:                
                dec  rcx      
                jnz  .i
    

Code:
mov  rcx,2_042_000_000
            .i:
                inc  eax      ;!* mean do parallel
                inc  r8       ;!*
                inc  r9       ;!*
                inc  edx      ;!*
                inc  ebx      ;!*
                inc  edi      ;!*
                dec  rcx      ;!*
                jnz  .i
    

I read AMD doing 4 instructions on tact . Ryzen 3500 (Zen2). 4 Ghz
But this code show 8 instruction on tact. I'm lost .

If i write in code couple regs. Like as inc esi and inc r10 i get different time.
Post 14 Aug 2021, 05:50
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20628
Location: In your JS exploiting you and your system
revolution 14 Aug 2021, 06:00
How many cycles were used to execute the loop? Use RDTSC to get the cycle count.

If it takes 4 cycles per loop, then the second loop only needs to execute 2 IPC to get the same timing.
Post 14 Aug 2021, 06:00
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.