-
Notifications
You must be signed in to change notification settings - Fork 56
/
TimeThingResult.cs
61 lines (55 loc) · 2.25 KB
/
TimeThingResult.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
// Copyright (c) 2020 Jon P Smith, GitHub: JonPSmith, web: http://www.thereformedprogrammer.net/
// Licensed under MIT license. See License.txt in the project root for license information.
namespace TestSupport.EfHelpers
{
/// <summary>
/// Result from a TimeThings instance once it is disposed
/// </summary>
public class TimeThingResult
{
/// <summary>
/// Creates the TimeThingResult
/// </summary>
/// <param name="totalTimeMilliseconds"></param>
/// <param name="numRuns"></param>
/// <param name="message"></param>
public TimeThingResult(double totalTimeMilliseconds, int numRuns, string message)
{
TotalTimeMilliseconds = totalTimeMilliseconds;
NumRuns = numRuns;
Message = message;
}
/// <summary>
/// Total time in milliseconds, with fractions
/// </summary>
public double TotalTimeMilliseconds { get; private set; }
/// <summary>
/// Optional number of runs. zero if not set.
/// </summary>
public int NumRuns { get; private set; }
/// <summary>
/// Optional string to identify this usage of the TimeThings
/// </summary>
public string Message { get; private set; }
/// <summary>
/// Provides a detailed report of the timed event
/// </summary>
/// <returns></returns>
public override string ToString()
{
var prefix = NumRuns > 1 ? $"{NumRuns:#,###} x " : "";
var suffix = NumRuns > 1 ? $", ave. per run = {TimeScaled(TotalTimeMilliseconds / NumRuns)}" : "";
return $"{prefix}{Message} took {TotalTimeMilliseconds:#,###.00} ms.{suffix}";
}
private string TimeScaled(double timeMilliseconds)
{
if (timeMilliseconds > 5 * 1000)
return $"{timeMilliseconds / 1000:F3} sec."; //Seconds
if (timeMilliseconds > 5)
return $"{timeMilliseconds:#,###.00} ms."; //Milliseconds
if (timeMilliseconds > 5 / 1000.0)
return $"{timeMilliseconds * 1000:#,###.00} us."; //Microseconds
return $"{timeMilliseconds * 1000_000:#,###.0} ns."; //Nanoseconds
}
}
}