3

我有这样的代码,如果我反复做某事,我想获得完成该任务所需的平均时间。

        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);

但是我在倒数第二行收到一个错误,说我无法将方法组“Average”转换为“System.TimeSPan”

4

2 回答 2

5

尝试这个:

TimeSpan timeaverage = TimeSpan.FromMilliseconds(TimeDisplay.Average(i=>i.TotalMilliseconds));
于 2013-07-16T16:37:06.873 回答
1
TimeSpan timeaverage = TimeSpan.FromTicks(Convert.ToInt64(TimeDisplay.Average(ts => ts.Ticks)));
于 2013-07-16T16:45:55.900 回答