internal sealed class OperationTimer : IDisposable
{
private Stopwatch m_stopwatch;
private string m_text;
private int m_collectionCount;
public OperationTimer(string text)
{
PrepareForOperation();
m_text = text;
m_collectionCount = GC.CollectionCount(0);
m_stopwatch = Stopwatch.StartNew();
}
public void Dispose()
{
Console.WriteLine("{0}详情", m_text);
int f = GC.CollectionCount(0) - m_collectionCount;
Console.WriteLine("垃圾回收次数:" + f);
Console.WriteLine("所用的时间:" + m_stopwatch.Elapsed);
}
private static void PrepareForOperation()
{
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
}
}
{
private Stopwatch m_stopwatch;
private string m_text;
private int m_collectionCount;
public OperationTimer(string text)
{
PrepareForOperation();
m_text = text;
m_collectionCount = GC.CollectionCount(0);
m_stopwatch = Stopwatch.StartNew();
}
public void Dispose()
{
Console.WriteLine("{0}详情", m_text);
int f = GC.CollectionCount(0) - m_collectionCount;
Console.WriteLine("垃圾回收次数:" + f);
Console.WriteLine("所用的时间:" + m_stopwatch.Elapsed);
}
private static void PrepareForOperation()
{
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
}
}