-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMethodTimer.cs
More file actions
166 lines (154 loc) · 4.97 KB
/
MethodTimer.cs
File metadata and controls
166 lines (154 loc) · 4.97 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
using System;
using System.Diagnostics;
using System.Threading.Tasks;
namespace TempFileCleaner
{
/// <summary>
/// <code>
/// Timing a standard method:
/// MethodTimer.Time(() => SomeHeavyWork(), "ProcessData");
///
/// Timing a method with a return value:
/// var result = MethodTimer.Time(() => CalculateTotal(), "CalculateTotal");
///
/// Timing an async operation (Most common in WPF):
/// await MethodTimer.TimeAsync(async () => {
/// await Task.Delay(500); // Simulate work
/// UpdateUI();
/// }, "AsyncUpdate");
/// </code>
/// </summary>
public static class MethodTimer
{
// You could point this to a debugger, logger, status bar, or a file.
public static Action<string> LogHandler { get; set; } = (message) => Debug.WriteLine(message);
/// <summary>
/// Times a void method.
/// </summary>
public static void Time(Action action, string methodName)
{
var sw = Stopwatch.StartNew();
try
{
action();
}
finally
{
sw.Stop();
Report(methodName, sw.Elapsed);
}
}
/// <summary>
/// Times a method that returns a value.
/// </summary>
public static T Time<T>(Func<T> func, string methodName)
{
var sw = Stopwatch.StartNew();
try
{
return func();
}
finally
{
sw.Stop();
Report(methodName, sw.Elapsed);
}
}
/// <summary>
/// Times an asynchronous Task.
/// </summary>
public static async Task TimeAsync(Func<Task> taskFunc, string methodName)
{
var sw = Stopwatch.StartNew();
try
{
await taskFunc();
}
finally
{
sw.Stop();
Report(methodName, sw.Elapsed);
}
}
/// <summary>
/// Times an asynchronous Task that returns a value.
/// </summary>
public static async Task<T> TimeAsync<T>(Func<Task<T>> taskFunc, string methodName)
{
var sw = Stopwatch.StartNew();
try
{
return await taskFunc();
}
finally
{
sw.Stop();
Report(methodName, sw.Elapsed);
}
}
static void Report(string methodName, TimeSpan elapsed)
{
// Formats output like: [TIMER] LoadSettings took 42ms
LogHandler?.Invoke($"{methodName} took {elapsed.TotalMilliseconds:N2}ms");
}
}
/// <summary><code>
/// // 1. Synchronous timing
/// var elapsedSync = MethodTimer.Time(() => Thread.Sleep(100));
/// Debug.WriteLine($"Sync operation took: {elapsedSync.TotalMilliseconds}ms");
///
/// // 2. Asynchronous timing
/// var elapsedAsync = await MethodTimer.TimeAsync(async () => await Task.Delay(100));
/// Debug.WriteLine($"Async operation took: {elapsedAsync.TotalMilliseconds}ms");
///
/// // 3. Timing with a return value
/// var(data, time) = await MethodTimer.TimeAsync(async () => {
/// await Task.Delay(50);
/// return "Operation Complete";
/// });
/// Debug.WriteLine($"Result: {data} (Took: {time.TotalMilliseconds}ms)");
/// </code></summary>
public static class MethodTimerAsync
{
/// <summary>
/// Times a synchronous action.
/// </summary>
public static TimeSpan Time(Action action)
{
var sw = Stopwatch.StartNew();
action();
sw.Stop();
return sw.Elapsed;
}
/// <summary>
/// Times a synchronous function that returns a value.
/// </summary>
public static (T Result, TimeSpan Elapsed) Time<T>(Func<T> func)
{
var sw = Stopwatch.StartNew();
var result = func();
sw.Stop();
return (result, sw.Elapsed);
}
/// <summary>
/// Times an asynchronous task.
/// </summary>
public static async Task<TimeSpan> TimeAsync(Func<Task> taskFunc)
{
var sw = Stopwatch.StartNew();
await taskFunc();
sw.Stop();
return sw.Elapsed;
}
/// <summary>
/// Times an asynchronous task that returns a value.
/// </summary>
public static async Task<(T Result, TimeSpan Elapsed)> TimeAsync<T>(Func<Task<T>> taskFunc)
{
var sw = Stopwatch.StartNew();
var result = await taskFunc();
sw.Stop();
return (result, sw.Elapsed);
}
}
}