The following data members are required to test the time using the Timing class:
StartingTiming -- the start time for storing the code being tested is used;
Duration --- end time of the Code being tested.
Common Timing methods:
StartTime, stopTime.
The following uses the Timing class to compare the time when Arraylist and collection classes are used to store data.
Timing class
Class Timing {TimeSpan duration; public Timing () {duration = new TimeSpan (0); // initialization} public void stopSpan () // stop Timing {duration = Process. getCurrentProcess (). totalProcessorTime; stop timing} public void startTime () // start timing {GC. collect (); GC. waitForPendingFinalizers ();} public TimeSpan Result () {return duration ;}}
Test code time
Timing tText = new Timing (); if (radioButton1.Checked) {int num = int. parse (textBox1.Text); // use the Arraylist class to store data tText. startTime (); int [] Num = new int [num]; for (int I = 0; I <num; I ++) Num [I] = I; tText. stopSpan (); label1.Text = tText. result (). totalMilliseconds. toString ();} if (radioButton2.Checked) // use the collection class to store data {int num = int. parse (textBox2.Text); tText. startTime (); Collection MyCollection = new Collection (); for (int j = 0; j <num; j ++) MyCollection. add (j); tText. stopSpan (); label3.Text = tText. result (). totalMilliseconds. toString ();}
The program interface is as follows:
This is just a simple program. Let us know how to use Timing class to test the Code time. If you have any problems, we can point it out.