What is the most suitable type of performance counter for measuring runtime?

Let's say I have a Foo () method and I want to measure the time in milliseconds it took to execute what type of Windows performance counter I should use?

var stopwatch = new Stopwatch(); stopwatch.Start(); Foo(); stopwatch.Stop(); counter.RawValue = stopwatch.TotalMilliseonds; 

I am currently using NumberOfItems64, but it saves the last counter value if a new operation is not performed. Is desirable? Or should the counter go to zero as soon as the operation is completed? What type of meter would you choose in this situation and why?

+6
performance performancecounter perfmon
source share
4 answers

That sounds like a good candidate for AverageTimer32 . See this SO answer for more details.

+7
source share

To add to the previous answer, there is a very good article on both pre-prepared and custom-made performance counters in CodeProject ..

+1
source share

I usually use a stopwatch, but I also used p / invoke for kernel32.dll:

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

I have two methods: Start () and Stop ():

  public void Start() { Thread.Sleep(0); QueryPerformanceCounter(out startTime); } public void Stop() { QueryPerformanceCounter(out stopTime); } 

To request the duration between calls in Start () and Stop ():

  public double Duration { get { return (double)(stopTime - startTime)*1000 / (double)freq; } } 

Note that freq is defined in the constructor:

  public PerformanceTimer() { startTime = 0; stopTime = 0; if (QueryPerformanceFrequency(out freq) == false) { // not supported throw new Win32Exception(); } } 

For my needs, I did not notice a big difference, although I suggest you try and see what suits you.

Edit: I was able to find the source code from MSDN . It seems that there are some improvements made with .Net 1.1. A delivery message is what Microsoft claims this method provides nanosecond accuracy.

0
source share

Answer and question:

Answer: Low-tech work for me. Do you want microseconds? Copy it 10 ^ 6 times and use your watch.

Question: You ask, do you want to make the code faster? Then consider it .

0
source share

All Articles