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