C#性能测试垃圾回收与运行时间帮助

    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();
        }




    }

猜你喜欢

转载自blog.csdn.net/qq_15555767/article/details/75009707