Change Average statistic to Median

This commit is contained in:
Andrey Akinshin 2013-08-28 01:05:11 +07:00
Родитель 5e5df2b5db
Коммит dca61d5dad
4 изменённых файлов: 31 добавлений и 10 удалений

Просмотреть файл

@ -37,11 +37,11 @@ namespace BenchmarkDotNet
ConsoleHelper.NewLine();
ConsoleHelper.WriteLineHeader("Competition results:");
var nameWidth = tasks.Max(task => task.Name.Length) + 1;
var msWidth = tasks.Max(task => task.Info.Result.AverageMilliseconds.ToString().Length);
var msWidth = tasks.Max(task => task.Info.Result.MedianMilliseconds.ToString().Length);
foreach (var task in tasks)
ConsoleHelper.WriteLineStatistic("{0}: {1}ms [Error: {2:00.00}%]",
task.Name.PadRight(nameWidth),
task.Info.Result.AverageMilliseconds.ToString().PadLeft(msWidth),
ConsoleHelper.WriteLineStatistic("{0}: {1}ms [Error: {2:00.00}%]",
task.Name.PadRight(nameWidth),
task.Info.Result.MedianMilliseconds.ToString().PadLeft(msWidth),
task.Info.Result.Error * 100);
}
}

Просмотреть файл

@ -53,6 +53,7 @@
<Compile Include="BenchmarkInfo.cs" />
<Compile Include="BenchmarkRun.cs" />
<Compile Include="BenchmarkRunList.cs" />
<Compile Include="Extensions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

Просмотреть файл

@ -26,18 +26,18 @@ namespace BenchmarkDotNet
public void PrintStatistic()
{
ConsoleHelper.WriteLineStatistic("TickStats: Min={0}, Max={1}, Avr={2}, Diff={3:00.00}%",
MinTicks, MaxTicks, AverageTicks, Error * 100);
ConsoleHelper.WriteLineStatistic("MsStats: Min={0}, Max={1}, Avr={2}",
MinMilliseconds, MaxMilliseconds, AverageMilliseconds);
ConsoleHelper.WriteLineStatistic("TickStats: Min={0}, Max={1}, Median={2}, Diff={3:00.00}%",
MinTicks, MaxTicks, MedianTicks, Error * 100);
ConsoleHelper.WriteLineStatistic("MsStats: Min={0}, Max={1}, Median={2}",
MinMilliseconds, MaxMilliseconds, MedianMilliseconds);
}
public long MinTicks { get { return this.Min(run => run.ElapsedTicks); } }
public long MaxTicks { get { return this.Max(run => run.ElapsedTicks); } }
public long AverageTicks { get { return (long)this.Average(run => run.ElapsedTicks); } }
public long MedianTicks { get { return this.Median(run => run.ElapsedTicks); } }
public long MinMilliseconds { get { return this.Min(run => run.ElapsedMilliseconds); } }
public long MaxMilliseconds { get { return this.Max(run => run.ElapsedMilliseconds); } }
public long AverageMilliseconds { get { return (long)this.Average(run => run.ElapsedMilliseconds); } }
public long MedianMilliseconds { get { return this.Median(run => run.ElapsedMilliseconds); } }
public double Error
{
get { return (MaxTicks - MinTicks) * 1.0 / MinTicks; }

Просмотреть файл

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace BenchmarkDotNet
{
public static class Extensions
{
public static long Median<TSource>(this IEnumerable<TSource> source, Func<TSource, long> selector)
{
var list = source.Select(selector).ToList();
if (list.Count == 0)
throw new InvalidOperationException("Sequence contains no elements");
list.Sort();
if (list.Count % 2 == 1)
return list[list.Count / 2];
return (list[list.Count / 2 - 1] + list[list.Count / 2]) / 2;
}
}
}