如何计算for循环所用的总时间

Dha*_*jay -2 c# performance for-loop

我的机器细节:32位操作系统(win-7),双核,时钟速度:2.93Ghz,使用的语言= c#

我有循环

for ( long d = 0 d<= K  ; d++) 
{
    //no instrucitons
}
Run Code Online (Sandbox Code Playgroud)

如果K是任何长数.

计算完成此循环所需时间(以秒为单位)的公式是什么?

tur*_*rbo 10

您可以使用Stopwatch.Elapsed属性秒表类

using System;
using System.Diagnostics;
using System.Threading;
class Program
{
    static void Main(string[] args)
    {
        Stopwatch stopWatch = new Stopwatch();
        stopWatch.Start();
        for (long d = 0; d<= K; d++)   
        {      
        //do something  
        }         
        stopWatch.Stop();
        // Get the elapsed time as a TimeSpan value.
        TimeSpan ts = stopWatch.Elapsed;

        // Format and display the TimeSpan value.
        string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
            ts.Hours, ts.Minutes, ts.Seconds,
            ts.Milliseconds / 10);
        Console.WriteLine("RunTime " + elapsedTime);
    }
}
Run Code Online (Sandbox Code Playgroud)