4

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?

0

6 Answers 6

14

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); } 
Sign up to request clarification or add additional context in comments.

4 Comments

can you please tell me where i should place this code in my application which has number of forms.
Your Form has 2 events, Loading and Exiting. Start StopWatch with Loading and stop when Exiting. So that you can catch the total work time. Another solution is below: using DateTime.Now. You may stop StopWatch and resume as you wish so that you can get the current execution time, too.
I have little info about asp.net but u should use stopWatch.Start(); as the very first line in the very first function of your home page called. stopWatch.Stop(); should be called where u want the stopwatch to stop. Send elapsedTime string in some logging where u can check your average execution time.
@Kalaivani: I hope my answer becomes accepted answer as it answers all your queries.
2

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); 

4 Comments

you always get 0 with DateTime.Now in the same call.
No you dont. I just tried with a loop from 1 to 10000000, it printed 30.
In any case is not accurate, and the Now is spend a lot of conversion time inside it, at least you can use UtcNow, but this is inaccurate also. Try also the release mode to see...
This will subtract just the miilisecond parts of the two time values and result in any meaningless value between -1000 and 1000. I don't know how this could get 4 upvotes.
1

Use the Benchmarking Made Easy library

A very easy and straightforward way is to use Jon Skeets Benchmarking Made Easy in C# toolset. Even with StopWatch, you still find yourself writing a lot of code around each bit you want to benchmark.

The benchmark toolset makes this trivial: you just hand it one or more functions and give them variable input and they will run until finished. The results for each function can then be introspected or printed to screen.

Comments

0

Write a static class and write the above code in a static method... call that method where ever you want to use the timer

1 Comment

i need to measure execution time of an whole application. If my application has number of forms, where i have to place the code to measure whole application execution time?
0

You can use the built-in profiler for the same, it's available in VS2010 Premium and Ultimate under Main Menu -> Analyze -> Profiler

1 Comment

Just to note that the built-in profiler is only available in the Premium and Ultimate versions of VS2010.
-1
using system.diagnostic; class Program { static void main(String[] args){ Stopwatch Timer = new Stopwatch(); //here is your code Timer.Stop(); Console.Writeline("Time Taken:" +Timer.Elasped); } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.