@manhng

Welcome to my blog!

Measure time in C# .NET

July 7, 2021 08:47

Measure time in C# .NET (edit)

Two ways to measure time in C# .NET | Exercises in .NET with Andras Nemes (dotnetcodr.com)

Two ways to measure time in C# .NET

static void Main(string[] args)
{
DateTime start = DateTime.UtcNow;
Thread.Sleep(2000);
DateTime end = DateTime.UtcNow;
TimeSpan timeDiff = end - start;
Console.WriteLine(Convert.ToInt32(timeDiff.TotalMilliseconds)); // => 2005

Stopwatch stopwatch = new Stopwatch();

stopwatch.Start();
Thread.Sleep(2000);
stopwatch.Stop();

stopwatch.Start();
Thread.Sleep(3000);
stopwatch.Stop();

TimeSpan stopwatchElapsed = stopwatch.Elapsed;
Console.WriteLine(Convert.ToInt32(stopwatchElapsed.TotalMilliseconds)); // => 5014
}

Favorite Links

Medium

Stack Overflow - Where Developers Learn, Share, & Build Careers

C# Corner - Community of Software and Data Developers (c-sharpcorner.com)

CodeProject - For those who code

ASPSnippets: Code Snippets, Tutorials, Articles, Tips on ASP.Net SQL Server, Windows, C#, VB.Net, AJAX, jQuery, AngularJS and MVC.

DotNet - awesome (dotnetawesome.com)

JavaScript in Plain English

Categories

Recent posts