计算某一段程序消耗的内存和时间【C#】
生活随笔
收集整理的這篇文章主要介紹了
计算某一段程序消耗的内存和时间【C#】
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
為了保證效果,與java操作相同,先清除一下垃圾C#的用法是:【GC.Collect()】
using System; using System.Diagnostics; namespace Action {class Program{static void Main(string[] args){GC.Collect();//清理一下先Random ra = new Random();int count = 1000000;//100WDateTime start = DateTime.Now;for (int i = 0; i < count; i++){new String(""+i);}DateTime end = DateTime.Now;double usedMemory = Process.GetCurrentProcess().WorkingSet64 / 1024.0 / 1024.0;Console.WriteLine("耗時:" + (end - start).TotalMilliseconds + "毫秒");Console.WriteLine("消耗內存:" + usedMemory + "M");}} }做接口的時候一定好好測好了再上線,否則接口突然的來一個"刷子"就夠你搶。
總結
以上是生活随笔為你收集整理的计算某一段程序消耗的内存和时间【C#】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: switch与if效率实例解析·5年以下
- 下一篇: c# char unsigned_dll