Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get ticks from QueryPerformanceCounter in C#?

Tags:

c#

.net-4.0

I need to replace Stopwatch to avoid using getters for its properties. I am going to implement it using QueryPerformanceCounter. I only need ticks nothing else.

Can anyone provide a code snipet please, to get correct ticks (1/10000 of ms) or any other small but stable value.

Please note my server sets clock granularity down to 0.5 ms (not sure if it affects QueryPerformanceCounter) but just for you to know.

Please also note - I do not need a timer. I only need to measure time intervals between parts of code.

EDIT: to avoid confusion I really want to know what is lpPerformanceCount in QueryPerformanceCounter(out long lpPerformanceCount);

like image 754
Boppity Bop Avatar asked Mar 20 '12 15:03

Boppity Bop


1 Answers

[DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceCounter(out long lpPerformanceCount);

[DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceFrequency(out long lpFrequency);

Taken from http://www.codeproject.com/Articles/2635/High-Performance-Timer-in-C

Old but it should still work

EDIT: The internals of StopWatch actually use QueryPerformanceCounter, so using the managed code should provide identical results, with better compatibility.

like image 64
Matthew Avatar answered Oct 16 '22 16:10

Matthew