Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate execution time of c# application

Tags:

c#

How to calculate the execution time of c# application. I am having c# windows application in which i need to calculate the execution time and i dont have any idea where i have to proceed this. Can anyone please help me?

like image 572
Kalaivani Avatar asked Dec 05 '22 17:12

Kalaivani


2 Answers

use Stopwatch of System.Diagnostics

static void Main(string[] args)
{
    Stopwatch stopWatch = new Stopwatch();
    stopWatch.Start();
    Thread.Sleep(10000);
    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);
}
like image 97
Nikhil Agrawal Avatar answered Dec 10 '22 11:12

Nikhil Agrawal


You could, for instance, just use DateTime.Now before and after your execution and then subtract the milliseconds:

DateTime then = DateTime.Now;

// Your code here

DateTime now = DateTime.Now;

Console.WriteLine(now.Millisecond - then.Millisecond);
like image 36
Binsh Avatar answered Dec 10 '22 09:12

Binsh