DebareDaDauntless
DebareDaDauntless

Reputation: 521

TimeSpan average

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'

Upvotes: 3

Views: 4049

Answers (2)

Reda
Reda

Reputation: 2289

TimeSpan timeaverage = TimeSpan.FromTicks(Convert.ToInt64(TimeDisplay.Average(ts => ts.Ticks)));

Upvotes: 1

Luis
Luis

Reputation: 6001

try this:

TimeSpan timeaverage = TimeSpan.FromMilliseconds(TimeDisplay.Average(i=>i.TotalMilliseconds));

Upvotes: 5

Related Questions