C# 检测 代码耗时

static void SubTest()
{
    Stopwatch sw = new Stopwatch();
    sw.Start();
  
    //耗时巨大的代码
    
    sw.Stop();
    TimeSpan ts2 = sw.Elapsed;
    Console.WriteLine("Stopwatch总共花费{0}ms.", ts2.TotalMilliseconds);
}
第一种方法利用System.DateTime.Now
static void SubTest()
{
    DateTime beforDT = System.DateTime.Now;  

    //耗时巨大的代码
    
    DateTime afterDT = System.DateTime.Now;
    TimeSpan ts = afterDT.Subtract(beforDT);
    Console.WriteLine("DateTime总共花费{0}ms.", ts.TotalMilliseconds);
}

// 出处 忘记了。。。

猜你喜欢

转载自www.cnblogs.com/enych/p/11901439.html