C# 时间跨度平均值

C# 时间跨度平均值,c#,timespan,C#,Timespan,我有这样的代码,我反复做一些事情,我想得到完成这项任务所需的平均时间 List<TimeSpan> TimeDisplay = new List<TimeSpan>(); while(some condition) { Stopwatch sw = new Stopwatch(); sw.Start(); TimeSpan temp;

我有这样的代码,我反复做一些事情,我想得到完成这项任务所需的平均时间

        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);
List TimeDisplay=新建列表();
虽然(某些条件)
{
秒表sw=新秒表();
sw.Start();
时间跨度温度;
//做点什么\\
sw.Stop();
温度=短波经过时间
时间显示。添加(温度);
}
TimeSpan timeaverage=TimeDisplay.Average;
System.Console.Writeline(“{0}”,timeaverage);
但我在倒数第二行遇到一个错误,说我无法将方法组“Average”转换为“System.TimeSPan”

尝试以下方法:

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

需要将其反馈到一个
TimeSpan
TimeSpan.fromMillimes(…)
@PeterRitchie是的,我更改了它。)根据@Luis的回答:
TimeSpan-timeaverage=TimeSpan.frommilluses(TimeDisplay.Average(i=>i.totalmiluses))
我想如果您的任务不是并行运行的,那么使用秒表会更好:在每个任务开始时使用Start,在任务结束时使用Stop。你将有一个良好的准确性的总时间。你只需要将它除以之后的任务数。
TimeSpan timeaverage = TimeSpan.FromTicks(Convert.ToInt64(TimeDisplay.Average(ts => ts.Ticks)));