How to get ticks from QueryPerformanceCounter in C #?

I need to replace the stopwatch to avoid using getters for its properties. I am going to implement it using QueryPerformanceCounter. I only need checkmarks .

Can anyone provide a code snape to get the correct ticks (1/10000 ms) or any other small but stable value.

Please note: my server sets the clock dimension to 0.5 ms (not sure if this affects the QueryPerformanceCounter), but only for you.

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

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

+4
source share
5 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 StopWatch actually use a QueryPerformanceCounter , so using managed code should provide identical results with better compatibility.

+5
source

http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.gettimestamp.aspx

Stopwatch.GetTimestamp

You do not need to create an instance of the Stopwatch object; GetTimestamp should return the number of ticks regardless of its context.

+8
source

DO NOT use StopWatch to determine high resolution time without testing it. It has been several years since I tested it, but at that time it gave default permission for windows. You can say, because really, really fast things will take 0 or 10-15 milliseconds if it uses the default resolution for Windows (the default resolution is actually the resolution of the context switches of the stream, apparently the default clock is updated by each context switch, which is approximately every 10-15 milliseconds).

+1
source

NOT A DIRECT RESPONSE (but it is imperative to obtain the necessary support information):

Some of the answers provided are pretty good to directly answer your question. However, to accomplish what you are trying to do, I would like to add a couple of notes.

First, consider the time-consuming just-in-time (jit) compiler that occurs at runtime. Any code where you take the initial timestamp, then do things, and then take the final timestamp to subtract t2-t1 for delta for any function inside the material, if you include any functions that you have not yet called during the current process , then the first time you call it, you will pay for jit overhead to compile the bytecode into native code. In this case, the cost does not reflect the actual cost of the critical performance code at runtime, which is apparently often called the code that is often called, and yet the cost of jit is paid only once (the first time this process is called). So call the time code several times, throw away the first time and take the average.

Also, beware of the time taken to complete the garbage collector. If you're just playing, it may be possible and interesting to compare code that strictly avoids highlighting new objects. But sometimes this is not easy, especially when you call functions whose implementations you cannot change. In addition, I must provide you that the real world overhead of the production code cannot avoid the overhead of garbage collection, so getting a realistic figure should include this overhead. However, if your stuff contains code that allocates new objects, then it can start a garbage collector, which will be expensive, so be prepared for some potential outliers that you might want to throw away.

Secondly, good answers provided code for declaring external functions to be called from system dlls and what was great. The proper use of these signatures in the general case can be a nuisance, so I would like to mention a great resource for this: pinvoke.net. The QueryPerformanceCounter search gave me signatures that cut and cut from the answer already provided, and this is a great resource for any system calls you want to make.

http://pinvoke.net/search.aspx?search=QueryPerformanceCounter&namespace=[All]

0
source
 I don't remember from where I copied it, but this code works well for me: public class QueryPerfCounter { [DllImport("KERNEL32")] private static extern bool QueryPerformanceCounter(out long lpPerformanceCount); [DllImport("Kernel32.dll")] private static extern bool QueryPerformanceFrequency(out long lpFrequency); private long start; private long stop; private long frequency; double multiplier = 1.0e6; // usecs / sec public QueryPerfCounter() { if (QueryPerformanceFrequency(out frequency) == false) { // Frequency not supported throw new Win32Exception(); } } public void Start() { QueryPerformanceCounter(out start); } public void Stop() { QueryPerformanceCounter(out stop); } public double Duration(int iterations) { return ((((stop - start) * multiplier) / frequency) / iterations); } } 
-1
source

Source: https://habr.com/ru/post/1402468/


All Articles