-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTimedResult.cs
88 lines (71 loc) · 2.71 KB
/
TimedResult.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace Open.Diagnostics;
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0046:Convert to conditional expression")]
public struct TimedResult : IComparable<TimedResult>, IEquatable<TimedResult>
{
public TimedResult(string label, TimeSpan duration)
{
Label = label;
Duration = duration;
}
public TimedResult(string label, Stopwatch stopwatch) : this(label, stopwatch.Elapsed)
{
}
public readonly string Label;
public readonly TimeSpan Duration;
public override string ToString() => string.Format("{1} {0}", Label, Duration);
public static TimeSpan Measure(Action action)
{
var sw = Stopwatch.StartNew();
action();
sw.Stop();
return sw.Elapsed;
}
public static TimedResult Measure(string label, Action action) => new(label, Measure(action));
public static T Measure<T>(out TimeSpan duration, Func<T> action)
{
var sw = Stopwatch.StartNew();
var result = action();
sw.Stop();
duration = sw.Elapsed;
return result;
}
public static T Measure<T>(out TimedResult measurement, string label, Func<T> action)
{
var result = Measure(out var duration, action);
measurement = new TimedResult(label, duration);
return result;
}
public bool Equals(TimedResult other)
=> base.Equals(other) || Label == other.Label && Duration == other.Duration;
public int CompareTo(TimedResult other)
{
if (Label != other.Label) throw new InvalidOperationException("Comparing two timed results that are not the same label.");
if (Duration < other.Duration) return -1;
if (Duration > other.Duration) return +1;
return 0;
}
public static bool operator <(TimedResult tr1, TimedResult tr2) => tr1.CompareTo(tr2) < 0;
public static bool operator >(TimedResult tr1, TimedResult tr2) => tr1.CompareTo(tr2) > 0;
public static bool operator <=(TimedResult tr1, TimedResult tr2) => tr1.CompareTo(tr2) <= 0;
public static bool operator >=(TimedResult tr1, TimedResult tr2) => tr1.CompareTo(tr2) >= 0;
public static TimedResult operator +(TimedResult tr1, TimedResult tr2)
{
var label = tr1.Label;
if (label != tr2.Label) throw new InvalidOperationException("Adding two timed results that are not the same label.");
return new TimedResult(label, tr1.Duration + tr2.Duration);
}
public static TimedResult operator -(TimedResult tr1, TimedResult tr2)
{
var label = tr1.Label;
if (label != tr2.Label) throw new InvalidOperationException("Adding two timed results that are not the same label.");
return new TimedResult(label, tr1.Duration - tr2.Duration);
}
}
public static class TimedResultExtensions
{
public static TimedResult Sum(this IEnumerable<TimedResult> results) => results.Aggregate((a, b) => a + b);
}