-
Notifications
You must be signed in to change notification settings - Fork 0
/
BenchmarkCreator.cs
37 lines (32 loc) · 1.08 KB
/
BenchmarkCreator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using Android.Util;
using Kotlin.Jvm.Internal;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
namespace Benchmark
{
internal class BenchmarkCreator
{
public static void Create(string name, Action<int> toPerform, int repetitions)
{
List<double> times = new List<double>(repetitions);
Stopwatch sw = new Stopwatch();
for (int i = 0; i<repetitions; i++)
{
sw.Reset();
sw.Start();
toPerform(i);
sw.Stop();
times.Add(sw.Elapsed.TotalMilliseconds * 1000);
}
double average = times.Sum()/repetitions;
double variance = 0;
times.ForEach(x => variance += Math.Pow(x - average, 2));
variance /= repetitions;
var stdDev = Math.Sqrt(variance);
Log.Info("Benchmark", $"{name}:\tmin={times.Min()} µs\tavg={average} µs\tmax={times.Max()} µs\tstdDev={stdDev} µs");
}
}
}