From dca61d5dad27d65b372da873ac1866e93bf87db4 Mon Sep 17 00:00:00 2001 From: Andrey Akinshin Date: Wed, 28 Aug 2013 01:05:11 +0700 Subject: [PATCH] Change Average statistic to Median --- BenchmarkDotNet/BenchmarkCompetition.cs | 8 ++++---- BenchmarkDotNet/BenchmarkDotNet.csproj | 1 + BenchmarkDotNet/BenchmarkRunList.cs | 12 ++++++------ BenchmarkDotNet/Extensions.cs | 20 ++++++++++++++++++++ 4 files changed, 31 insertions(+), 10 deletions(-) create mode 100644 BenchmarkDotNet/Extensions.cs diff --git a/BenchmarkDotNet/BenchmarkCompetition.cs b/BenchmarkDotNet/BenchmarkCompetition.cs index 835584aa9..85db0554a 100644 --- a/BenchmarkDotNet/BenchmarkCompetition.cs +++ b/BenchmarkDotNet/BenchmarkCompetition.cs @@ -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); } } diff --git a/BenchmarkDotNet/BenchmarkDotNet.csproj b/BenchmarkDotNet/BenchmarkDotNet.csproj index 913f3aeb6..12eeee817 100644 --- a/BenchmarkDotNet/BenchmarkDotNet.csproj +++ b/BenchmarkDotNet/BenchmarkDotNet.csproj @@ -53,6 +53,7 @@ + diff --git a/BenchmarkDotNet/BenchmarkRunList.cs b/BenchmarkDotNet/BenchmarkRunList.cs index 7be0f2e8e..36f5df162 100644 --- a/BenchmarkDotNet/BenchmarkRunList.cs +++ b/BenchmarkDotNet/BenchmarkRunList.cs @@ -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; } diff --git a/BenchmarkDotNet/Extensions.cs b/BenchmarkDotNet/Extensions.cs new file mode 100644 index 000000000..90940a85e --- /dev/null +++ b/BenchmarkDotNet/Extensions.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace BenchmarkDotNet +{ + public static class Extensions + { + public static long Median(this IEnumerable source, Func 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; + } + } +} \ No newline at end of file