English 中文(简体)
What is the most appropriate performance counter type for measuring operation time?
原标题:

Say I have a method Foo() and I want to measure the time in milliseconds it took to execute which type of Windows Performance Counter should I be using?

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

Currently I m using NumberOfItems64 but that persists the last value of the counter unless the new operation is performed. Is it desirable? Or should the counter go to zero as soon as the operation is done? Which counter type would you choose in this situation and why?

最佳回答

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

问题回答

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

I normally use Stopwatch, but I have also used p/invoke to 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 called Start() and Stop():

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


  public void Stop()
  {
     QueryPerformanceCounter(out stopTime);
  }

To query the duration between calls to Start() and Stop():

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

Note that freq is determined in the constructor:

  public PerformanceTimer()
  {
     startTime = 0;
     stopTime = 0;

     if (QueryPerformanceFrequency(out freq) == false)
     {
        // not supported
        throw new Win32Exception();
     }
  }

For my needs, I have not noticed much of a difference, although I suggest you try both to see what suits you.

Edit: I was able to locate the original code from MSDN. It appears there are some improvements made since .Net 1.1. The take-home message is that Microsoft claims that this method provides nanosecond accuracy.

An answer and a question:

Answer: Low-tech works for me. You want microseconds? Loop it 10^6 times and use your watch.

Question: Are you asking because you want to make your code faster? Then consider this.





相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签