I'd like to do some basic profiling of my code, but found that the DateTime.Now in C# only have a resolution of about 16 ms. There must be better time keeping constructs that I haven't yet found.
相关问题
- Generic Generics in Managed C++
- How to Debug/Register a Permanent WMI Event Which
- 'System.Threading.ThreadAbortException' in
- Bulk update SQL Server C#
- Should I use static function in c# where many call
Here is a sample bit of code to time an operation:
EDIT:
And the neat thing is that it can resume as well.
The System.Diagnostics.Stopwatch class will use a high-resolution counter if one is available on your system.
You could call down to the high-resolution performance counter in Windows. The function name is QueryPerformanceCounter in kernel32.dll.
Syntax for importing into C#:
Syntax for Windows call:
QueryPerformanceCounter @ MSDN
For highest resolution performance counters you can use the underlying win32 performance counters.
Add the following P/Invoke sigs:
And call them using:
Dump it all into a simple class and you're ready to go. Example (assuming a class name of PerformanceCounters):
The System.Diagnostics.StopWatch class is awesome for profiling.
Here is a link to Vance Morrison's Code Timer Blog if you don't want to write your own measurement functions.