Test Time Using Timing in C #

Source: Internet
Author: User

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.

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.