C # Stopwatch Gets the time consumption of an operation in a loop

Source: Internet
Author: User

In C #, DateTime is typically used to represent the current time, and you can use a DateTime object to get the current time before and after an operation, and then subtract two DateTime objects to get the time difference (TimeSpan object), so that this operation takes a lot longer.

However, in some cases, a loop fetch is required to perform an operation, and this operation may not take much time, but the time spent is still considerable when the magnitude of the loop is large. It would be troublesome to use the method above.

C # provides a class stopwatch that invokes the start method of the Stopwatch object before the operation begins, and calls the Stop method of the Stopwatch object after the operation ends. At the end of the loop, the elapsed corresponding property of the stopwatch object can be called to get the total time.

Worksheet sheet =ExcelAddIn2.Globals.ThisAddIn.Application.ActiveSheet; Range Allcells=sheet.            Cells; Stopwatch Addresswatch=NewStopwatch (); Stopwatch Addresslocalwatch=NewStopwatch (); foreach(Range cellinchallcells)                {Addresswatch.start (); varAddress =cell.                Address;                Addresswatch.stop ();                Addresslocalwatch.start (); varAddressLocal =cell.                addresslocal;            Addresslocalwatch.stop (); }            DoubleAddresscoast = AddressLocalWatch.Elapsed.TotalMilliseconds;

C # Stopwatch Gets the time consumption of an operation in a loop

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.