B
B
Boris the Animal2016-05-18 12:14:02
Programming
Boris the Animal, 2016-05-18 12:14:02

Get the average value per second. How to do it more correctly?

Hello. I need to calculate the average of received data per second.
For example, there is a class:

class StateMonitor
    {
        private long _quantityOfValues;
        private long _quantityOfCalls;

        private readonly object _quantityOfValuesSync = new object();
        private readonly object _frequencySync = new object();

        public void Increment()
        {
            lock (_quantityOfValuesSync)
            {
                ++_quantityOfValues;
            }
        }

        public int Frequency
        {
            get
            {
                long quantityOfValues;
                lock (_quantityOfValuesSync)
                {
                    quantityOfValues = _quantityOfValues;
                }

                lock (_frequencySync)
                {
                    double result = (double)quantityOfValues / ++_quantityOfCalls;
                    result = Math.Round(result, MidpointRounding.AwayFromZero);
                    return (int)result;
                }
            }
        }
    }

In order for it to correctly calculate the frequency of calling the Increment method per second, you need to call the Frequency property 1 time per second. What can not be called convenient and reliable. Surely there is a formula for such a calculation. I guess that it will be necessary to save the time of the last call to the Frequency property in order to calculate correctly. Even I can’t finish it, how can I count correctly?
Here is the code for the class test above:
using System;
using System.Threading;
using System.Threading.Tasks;

namespace FrequencyReceivingTest2
{
    internal class Program
    {
        #region Entry point

        private static Program _program;

        private static void Main(string[] args)
        {
            _program = new Program();
            _program.Run(args);
        }

        #endregion

        private void Run(string[] args)
        {
            var monitor = new StateMonitor();
            int counter = 0;

            Task[] tasks =
            {
                new Task(() =>
                {
                    while (counter++ < 20)
                    {
                        int ms = 0;
                        while (ms < 1000)
                        {
                            const int WaitMs = 200;
                            monitor.Increment();
                            Thread.Sleep(WaitMs);
                            ms += WaitMs;
                        }
                    }
                }),

                new Task(() =>
                {
                    while (counter < 20)
                    {
                        Console.WriteLine("Частота обновления: {0}/сек. ", monitor.Frequency);
                        Thread.Sleep(1000);
                    }
                }),
            };

            Array.ForEach(tasks, task => task.Start());

            Task.WaitAll(tasks);
            Console.WriteLine("Конец.");
            Console.ReadKey();
        }
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
#
#algooptimize #bottize, 2016-05-18
@user004

On mobile, hard to see. There are two options for calculating the frequency.
1 for the last second, count the frequency, hold the queue, delete what lives longer than 1 second, add new ticks, recalculate the frequency, this is the length of the queue. Option 2 is more difficult. The same, but at different time intervals (less than 1 sec in terms of 1 sec), if necessary.
For example 2 events in 200 ms, that's 10 events in 1 second. Everything is abstract.

R
res2001, 2016-05-18
@res2001

Store the timestamp of the first and last call to Increment. You can easily calculate the number of calls per second on demand: quantityOfValues/(tend-tbegin). (tend-tbegin) needs to be converted to seconds of course.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question