-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathProfiler.cs
38 lines (31 loc) · 1.13 KB
/
Profiler.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
using System;
using System.Diagnostics;
namespace PracticeQuestionsSharp.Helper
{
public static class Profiler
{
public static T Execute<T>(Func<T> method)
{
return method();
}
//Accepts a function and logs the execution time.
// Usage: Profiler.ProfileAndExecute(() => yourMethod(args), repeat?, name?);
public static T ProfileAndExecute<T>(Func<T> method, int repeat = 1, string name = "")
{
T result = default(T);
long avg, total;
Console.WriteLine($"Starting method({name})...");
Stopwatch watch = Stopwatch.StartNew();
for (int i = 0; i < repeat; ++i)
result = method();
watch.Stop();
total = watch.ElapsedMilliseconds;
avg = total / repeat;
Console.WriteLine($"Result: {result}");
Console.WriteLine($"Total of {total} milliseconds elapsed.");
Console.WriteLine($"Average of {avg} milliseconds elapsed. (Run {repeat} times).\n");
return result;
}
//TODO: allow void return type
}
}