方法执行时间不显示

Method execution time not showing

我正在尝试测量方法执行时间,但它没有显示执行时间。谁能帮我解决问题?

public static int SequentialSearch(int[] point, int findPoint)
{
    DateTime start = DateTime.Now;
    int i;
    for (i = 0; i < point.Length; i++)
    {
        if (findPoint== point[i])
        {
            return i;
        }
    }
    DateTime end = DateTime.Now;
    TimeSpan ts = (end - start);
    Console.WriteLine("Elapsed Time is {0} ms", ts.TotalMilliseconds);
    return -1;
}

实际上有更好的方法,使用StopWatch

//You need this using statement
using System.Diagnostics;
       
//Set it up, and then start it.
var timer = new Stopwatch();
timer.Start();

//Run your code
MethodToTime();

//Now stop the timer
timer.Stop();


//You can output either directly the elapsed MS
Console.WriteLine($"RunTime {timer.ElapsedMilliseconds}ms");

//Or you can get the elasped time span and output
TimeSpan ts = stopWatch.Elapsed;
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);