Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recommended way to calulate time and display to user [closed]

Tags:

c#

c#-4.0

Just curious as to what people think is the best way to calculate how long a search takes and then display time to user.

I am using DateTime with Timespan and also Stopwatch sw = new Stopwatch.

I think using Stopwatch is cleaner code compared to DateTime with timespan.

So I'm just curious as to what professional programmers use.

i.e

Stopwatch WebSearchTime = new Stopwatch();
WebSearchTime.Start();
code to run
WebSearchTime.Stop();
double WST = WebSearchTime.Elapsed.Seconds + (WebSearchTime.Elapsed.Milliseconds / 1000.0);

Thanks

George

like image 643
CareerChange Avatar asked Feb 03 '26 11:02

CareerChange


1 Answers

Stopwatch is the proper and more accurate way to determine time of execution, than DateTime.Now with DateTime calculation.

Stopwatch Class

The Stopwatch measures elapsed time by counting timer ticks in the underlying timer mechanism. If the installed hardware and operating system support a high-resolution performance counter, then the Stopwatch class uses that counter to measure elapsed time. Otherwise, the Stopwatch class uses the system timer to measure elapsed time. Use the Frequency and IsHighResolution fields to determine the precision and resolution of the Stopwatch timing implementation.

like image 128
Habib Avatar answered Feb 04 '26 23:02

Habib



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!