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