Contents

 

如下,分别计算时间和内存。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public static double TimeWatcher(Action action)
{
System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
watch.Start();
action();
watch.Stop();
var useTime = (double) watch.ElapsedMilliseconds/1000;
return useTime;
}

public static long MemoryWatcher(Action action)
{
long start = GC.GetTotalMemory(true);
action();
GC.Collect();
GC.WaitForFullGCComplete();
long end = GC.GetTotalMemory(true);
long useMemory = (end - start)/(1024*1024);
return useMemory;
}
Contents