none
AverageTimer32 or AverageCount64. What to choose? RRS feed

  • Question

  • I’m trying already few days to perform pretty simple task: we want measure how much time takes to operation to perform in average. We want to store it in performance counter and use it immediately or later. I found some examples, but I still not sure what to do.

    Lets skip all counter’s initialization stuff and this what I got:

    PC = new PerformanceCounter("Category", "My timer", false);

    BPC = new PerformanceCounter("Category", " My timer Base", false);

     

    PC.RawValue = 0;

    BPC.RawValue = 0;

     

    I may calculate operation time next way for example:

     

    Stopwatch s = Stopwatch.StartNew();

    // do something

    s.Stop();

     

     

    And from this point I have doubts:

    Since Performance Counter gets only long numbers we may count or elapsed milliseconds or elapsed ticks. Lets choose this

     

    long elapsedMilliseconds = s.ElapsedMilliseconds;

     

    Question what to choose AverageTimer32 or AverageCount64? I can’t understand what they give to me. If I’m choosing for AverageCount64 I will write something like this.

     

    On each new value:

    PC.IncrementBy(elapsedMilliseconds);

    BPC.Increment();

     

    And later when I want current value I may write:

    CounterSample sample = PC.NextSample();

    long average = sample.RawValue/sample.BaseValue;

     

    Exactly the same code I may write with AverageTimer32 counter.

     

    Questions:

    1)  Did I write correct code?

    2)   Do I need to perform average calculation by myself?

    3)    How AverageTimer32 can improve this code, since it ‘s pretty strange for me to calculate average by myself?

     

    Thanks in advance…

    Sunday, April 27, 2008 9:48 AM

All replies

  • yevgenyd,

     

    --"Did I write correct code?"

     

    The ElapsedMilliseconds Property value is the read-only long integer while the IncrementBy method return value is the same type.

     

    -- "Do I need to perform average calculation by myself?"

     

    If you do not need the accuracy that interlocked operations provide, you can update the RawValue property directly for up to a 5 times performance improvement. In my opinion, this thread on TickCount can help you to understand this issue better.

     

    --"How AverageTimer32 can improve this code, since it's pretty strange for me to calculate average by myself?"

     

    As the MSDN document mentioned:

     

    AverageTimer32: An average counter that measures the time it takes, on average, to complete a process or operation. Counters of this type display a ratio of the total elapsed time of the sample interval to the number of processes or operations completed during that time. This counter type measures time in ticks of the system clock.

     

    AverageCount64: An average counter that shows how many items are processed, on average, during an operation. Counters of this type display a ratio of the items processed to the number of operations completed. The ratio is calculated by comparing the number of items processed during the last interval to the number of operations completed during the last interval.

     

    In my opinion, it is better to try the example provided both AverageTimer32 and AverageCount64, then make a comparison in the link below:

     

    http://msdn2.microsoft.com/en-us/library/system.diagnostics.performancecountertype.aspx

     

    Then you could perform this example from code project to see the Using AverageTimer32 And AverageBase in the real project.

     

    Hope that can provide you some idea.

    Tuesday, April 29, 2008 7:47 AM
  • First of all thank you for answer.

    Unfortunately I still didn’t get answer for question.

    I’m already familiar with link that you sent to me:

    http://msdn2.microsoft.com/en-us/library/system.diagnostics.performancecountertype.aspx

    Here in AverageTimer32 example Perf.Counter sample data collected in array and later processed using CounterSample.Calculate(…) method.

    In my case I’m just want to update from time to time my performance counter and in any moment of time print average time till now.

    And as I wrote before I just need to do next:

     

    CounterSample sample = PC.NextSample();

    long average = sample.RawValue/sample.BaseValue;

     

    It brings me back to my question: AverageTimer32 or AverageCount64?

    And if AverageTimer32 what is the advantages?

     

    P.S. why in AverageTimer32 example form the link above in method CollectSamples(…)
    BPC.IncrementBy(10); Why 10 and not 1?

     

    Tuesday, April 29, 2008 10:56 AM