C # COMPUTE code Execution time-consuming

Source: Internet
Author: User
Tags time and date

The first method: System.DateTime.Now

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceexample{ Public classProgram {Static voidMain (string[] args) {            intsum=0; DateTime Beforedt = System.DateTime.Now;  for(inti =0; I <100000; i++) {sum+=i; } DateTime Afterdt = System.DateTime.Now;//gets a System.DateTime object that is set to the current date and time on this computer, expressed as local time            TimeSpan dt = afterdt.subtract (Beforedt);//a timespan represents a time interval in which Subtract () subtracts the specified time and date from this instanceConsole.WriteLine ("program time: ' {0} ' seconds", DT);        Console.ReadLine (); }    }}

The second method: Stopwatch

usingSystem;usingSystem.Collections.Generic;usingSystem.Diagnostics;usingSystem.Linq;usingSystem.Text;namespaceexample{ Public classProgram {Static voidMain (string[] args) {            intsum=0; Stopwatch SW = new Stopwatch ();//Stopwatch provides a set of methods and properties that can be used to accurately measure run timesSW.            Start ();  for(inti =0; I <100000; i++) {sum+=i; } SW.            Stop (); TimeSpan DT = SW. Elapsed;//gets the total elapsed time measured by the current instanceConsole.WriteLine ("program time: ' {0} ' seconds", DT);        Console.ReadLine (); }    }}

C # COMPUTE code Execution time-consuming

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.