High Resolution Performance Counter?

elkinm

Platinum Member
Jun 9, 2001
2,146
0
71
I am trying to implement a high resolution timer in C# with VS 2008.

I have found two websites with code examples here:
http://www.codeproject.com/KB/cs/highperformancetimercshar.aspx
http://www.cambiaresearch.com/c4/803908d2-ab13-4b90-9d73-6c940e70d9bd/csharp-StopWatch-Class.aspx

I should mention that I never used VS studio or C# which may explain my questions.

The provide code does work, however what I need is a series of time points through my code such as were part1 begins and ends, part 2 begins and ends and so on and so I can measure the time between any of the different sections.

I though of adding a method to the class that returns the frequency and the time at any point and have my code calculate all the time differences internally.

Do I need to keep it in a class? Meaning can I call the performance counter directly from my code?
They both use DLLImport("Kernel32.dll") is that how the functions are initialized?

Also, the first site gives the time in seconds, while the second one says the result is in tens of milliseconds and multiplies it by 10000. Which one is correct?
My machine is 2.66 GHz and the frequency is about 2.66 billion, so I would think the first way is correct.
Also, if I replace the frequency in my code to 1/fequency and multiply, will I loose precision, and will the performance improvement be work it?

I know there is overhead with the performance timer, is there a known range as to how many cycles it takes to give the result.

Sorry for all these questions. I would greatly appreciate any help I can get.

Thank you,
elkinm
 

elkinm

Platinum Member
Jun 9, 2001
2,146
0
71
Thanks, I have not seen this class before. I will try it, but I am not sure it would be enough as I need a way to time between any of the data points. From what I read the stopwatch seems more like a lap timer, I am not sure it can give a time for the entire race, or any of the laps or any combination of laps.

The perfect way would be to write a log with each of the entries and then have a separate parser give the results. I am afraid that the logging may be to slow.

I can do a debug.print with each time call, can't calculate all the results. I though of creating and array or list to record all the times that I can access at any time.
Once again, I am not sure this would be efficient.

On a slightly different note, can somebody explain how to convert the high resolution time to actual time of day. I know the counter time drifts and there are several synchronization algorithms online, but I still don't understand how to get the actual time?

Thanks again.
 

scootermaster

Platinum Member
Nov 29, 2005
2,411
0
0
Oh, and here I thought you were talking about something like PAPI.

But then I saw things like Visual Studio and C# and I got scared.

I'll go crawl back into my ivory tower now.
 

elkinm

Platinum Member
Jun 9, 2001
2,146
0
71
I was able to successfully get the timers working. However, I am not sure what is the actual time generated.

I did a simple test test of parsing comma delimited stings with C# code and discovered that it seems to take 1 ms or so per command which seems way to slow. Some sites list multiplying the frequency by 10000 to get time per second.
As I said before, my frequency is about 2.66 billion which is right for a 2.66 GHz processor.

Then I tried querying the performance counter in consecutive lines and the resulting time is about 1 micro-second, which is much faster, but still seems to slow for one call.

I have been unable to pause or suspend the thread (can't find how) for lets say a second to confirm the exact timer values.

Any ideas or explanation for my performance results?
 

Cogman

Lifer
Sep 19, 2000
10,284
138
106
Threads are paused with Sleep or SuspendThread.

as for what the performance counter is measuring, it is most likely the number of clocks the CPU completes (though, the specs say it could be something else). Use QueryPreformanceFrequency to find out how many ticks / second