Getting Started with Web applications development with servlets and JSP - Part 5: Filters
First Time? You can support us by signing up. It takes only 5 seconds. Click here to sign up. If you already have an account, click here to login.


Upload Image Close it
Select File

Share a .NET tip or learn from what others have shraed.

C# - Stopwatch class will give simple solution to calculate execution time of code

Dec 26 2011 10:49PM by Ramireddy   

Recently couple of times, I have a requirement of showing execution time to the users. Both times, I followed the approach of taking a starttime variable and stop time variable and calculating difference between those variables.

Today I saw an MSDN blog, which reminds me about the class "Stopwatch", which is specifically designed for this purpose. By using this, we can efficiently find the execution time of the code and format the result with ease.

      using System.Diagnostics;
      .......
       Stopwatch objWatch = new Stopwatch();
        objWatch.Start();
        // put your execution code here
        objWatch.Stop();
        TimeSpan ts = objWatch.Elapsed;
        string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
            ts.Hours, ts.Minutes, ts.Seconds,
            ts.Milliseconds / 10);
        Response.Write("RunTime " + elapsedTime);
Read More..  [4786 clicks]


Ramireddy
3 · 39% · 8882
15
 
5
 
12
 
0
Incorrect
 
0
Interesting
 
0
Forgotten
 
0
Move



Submit

Your Comment


Sign Up or Login to post a comment.

"C# - Stopwatch class will give simple solution to calculate execution time of code" rated 5 out of 5 by 15 readers
C# - Stopwatch class will give simple solution to calculate execution time of code , 5.0 out of 5 based on 15 ratings
    Copyright © Beyondrelational.com Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising