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

来源:互联网 发布:java 数据类型 赋值 编辑:程序博客网 时间:2024/04/28 05:29
    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();
        }




    }
原创粉丝点击