. net/c# measuring run time with stopwatch

Source: Internet
Author: User

Stopwatch class: Http://msdn.microsoft.com/zh-cn/library/system.diagnostics.stopwatch (v=vs.100). aspx

Common Properties and methods:

Start (): Starts or continues to measure the elapsed time of an interval.

Stop (): Stops measuring the elapsed time of an interval.

Elapsedmilliseconds: Gets the total elapsed time (in milliseconds) measured by the current instance.

Example:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Diagnostics;namespaceboxandunbox{classProgram {Static voidMain (string[] args) {Stopwatch Watch=NewStopwatch (); Watch.            Start (); Longresult =Sumwithoutbox (); Watch.            Stop (); Console.WriteLine ("the Sumwithoutbox () method returns the result of the calculation: {0}, spents {1} milliseconds", result, watch. Elapsedmilliseconds);//gets the total elapsed time (in milliseconds) measured by the current instanceWatch.            Restart (); Result=Sumwithbox (); Watch.            Stop (); Console.WriteLine ("the Sumwithbox () method returns the result of the calculation: {0}, spents {1} milliseconds", result, watch.            Elapsedmilliseconds);        Console.readkey (); }        Static LongSumwithoutbox () {Longsum =0;  for(Longi =0; I <10000000; i++) Sum+=i; returnsum; }        Static LongSumwithbox () {Objectsum =0L;//Packing             for(Longi =0; I <10000000; i++) Sum= (Long) sum + i;//unpacking, summing, and packing first            return(Long) sum;//Unpacking        }    }}

------------>>>

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.