C # code running timing

Source: Internet
Author: User

1 public class OperationTimer: IDisposable
2 {
3 private Int64 m_startTime;
4 private string m_text;
5 private Int32 m_CollectionCount;
6
7 public OperationTimer (string text)
8 {
9 m_startTime = Stopwatch. GetTimestamp ();
10 m_text = text;
11}
12
13 public void Dispose ()
14 {
15 string text = string. Format ("Time = {0, 6: ###. 00}", (Stopwatch. GetTimestamp ()-m_startTime)/Stopwatch. Frequency );
16 Console. WriteLine (string. Format ("{0 }:{ 1}", m_text, text ));
17}
18}

I tried it and it worked very well. The program is very simple. It records the start time in the class constructor of the class inherited from the IDisposable interface. After calculating the time difference during destruction, you can get the corresponding execution time. It is also very clever to use, directly using (OperationTimer op = new OperationTimer ("Generic") {...}, very nice and concise to implement the desired function. However, there is a small case, that is, the code written in asp.net at ordinary times. If it is not convenient to run on the console every time, if it can be modified, this class can be used in both the console and the asp.net environment. After reading the implementation principle of this class, because the runtime time difference is calculated only when the class is destroyed, there is no more space to get the time. It seems that there is only one delegate, so that the program will be executed during destruction and the time will be transferred to the desired place. Well, I 've been writing a program for so long, and I 've also read many commissions. Because I have never used it in practice, I'm not very familiar with the writing, so I just want to repeat it. The following is the transformation process. Step 1: Modify the name of this class according to your own method, and add a constructor for simple calling. The Code is as follows: /// <Summary>
/// Calculation execution time class
/// Modify: added the delegate Method
/// Time: 2009-12-8
/// </Summary>

Class CodeTimer: IDisposable
{
Private Int64 & n

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.