Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TimeSpan average

Tags:

c#

timespan

I have code like this were I am repeatedly doing something and I want to get the avergae time it takes to complete that task.

        List<TimeSpan> TimeDisplay = new List<TimeSpan>();

        while(some condition)
        {
            Stopwatch sw = new Stopwatch();
                  sw.Start();
            TimeSpan temp;


            //DO SOMETHING\\

            sw.Stop();
            temp = sw.Elapsed
            TimeDisplay.Add(temp);

        }

        TimeSpan timeaverage = TimeDisplay.Average;

        System.Console.Writeline("{0}", timeaverage);

But I get an error on the second to last line saying I can't convert method group 'Average' to 'System.TimeSPan'

like image 291
IkeJoka Avatar asked Jul 26 '26 19:07

IkeJoka


2 Answers

try this:

TimeSpan timeaverage = TimeSpan.FromMilliseconds(TimeDisplay.Average(i=>i.TotalMilliseconds));
like image 177
Luis Avatar answered Jul 28 '26 08:07

Luis


TimeSpan timeaverage = TimeSpan.FromTicks(Convert.ToInt64(TimeDisplay.Average(ts => ts.Ticks)));
like image 45
Reda Avatar answered Jul 28 '26 09:07

Reda



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!