Im trying to measure method execution time, but it didn't show the execution time. Can anyone help me to solve the problem?
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;
}
There is actually a better way to do this, use 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);