-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathUtil.cs
31 lines (26 loc) · 988 Bytes
/
Util.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
using System;
using System.Diagnostics;
namespace CSharp2013
{
public static class Utility
{
public static void Timeit(string name, int times, Action action)
{
// JIT、Warm up cache
if (times > 1) action();
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
var gcCounts = new int[GC.MaxGeneration + 1];
for (var i = 0; i < gcCounts.Length; ++i) gcCounts[i] = GC.CollectionCount(i);
var stopwatch = new Stopwatch();
stopwatch.Start();
for (var i = 0; i < times; ++i) action();
stopwatch.Stop();
for (var i = 0; i < gcCounts.Length; ++i) gcCounts[i] = GC.CollectionCount(i) - gcCounts[i];
Console.WriteLine(
"{0,-24} => Time: {1:0.######}ms, GC counts: {2}",
name,
stopwatch.Elapsed.TotalMilliseconds/times,
string.Join(",", gcCounts));
}
}
}