Performance of the function of accurate measurement: The method that surpasses datetime.now
When looking for performance bottlenecks, it is important to choose the right time method. Many developers depend on to measure functional performance. But is this the most accurate way?
The use of may have defects. DateTime.Now
usually involve time processing and adjustment, such as time zone conversion and summer time (DST) considerations. This expenses will be delayed to the measurement process.
In order to obtain more accurate performance evaluation, consider using the DateTime.Now
class in the DateTime.Now
.
. The following code fragment demonstrate its usage: System.Diagnostics
Stopwatch
Stopwatch
The availability of high -precision timers on the hardware on the hardware and use them accordingly. In addition, DateTime.Now
usually has a higher resolution than
<code class="language-csharp">Stopwatch sw = Stopwatch.StartNew(); // 执行操作 sw.Stop(); Console.WriteLine("耗时:{0}ms", sw.Elapsed.TotalMilliseconds);</code>
In short, although Stopwatch
can provide basic timing functions, DateTime.UtcNow
provides higher accuracy and reliability, making it the first choice for precise evaluation function performance. DateTime.Now
The above is the detailed content of Is `DateTime.Now` the Best Way to Measure Function Performance?. For more information, please follow other related articles on the PHP Chinese website!