measureRunTime use DispatchTime (#2545)

Motivation:

Using `Date()` in benchmarks leaves us open to the possibility that a
clock update could occur between the start and end times affecting the
measured interval.

Modifications:

Switch `measureRunTime` and related functions to use `DispatchTime`
uptime in nanoseconds to calculate intervals

Result:

Tests should no longer be susceptible to clock updates.
This commit is contained in:
Rick Newton-Rogers authored and GitHub committed 2023-10-11 02:58:16 -07:00
1 parent 8c3cac7774
commit fca6ecfdb0
1 file changed
+3 -3
@@ -19,10 +19,10 @@ import Foundation
public func measureRunTime(_ body: () throws -> Int) rethrows -> TimeInterval {
func measureOne(_ body: () throws -> Int) rethrows -> TimeInterval {
let start = Date()
let start = DispatchTime.now().uptimeNanoseconds
_ = try body()
let end = Date()
return end.timeIntervalSince(start)
let end = DispatchTime.now().uptimeNanoseconds
return Double(end - start)/1_000_000
}
_ = try measureOne(body)