Skip to content

Commit 65f05b3

Browse files
committed
Add Timer class for portability between Darwin and POSIX platforms
1 parent d945522 commit 65f05b3

File tree

1 file changed

+26
-33
lines changed

1 file changed

+26
-33
lines changed

benchmark/utils/DriverUtils.swift

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -230,29 +230,15 @@ func stopTrackingObjects(_: UnsafeMutableRawPointer) -> Int
230230

231231
#endif
232232

233-
protocol SampleRunner {
234-
func run(_ name: String, fn: (Int) -> Void, num_iters: UInt) -> UInt64
235-
}
236-
237233
#if os(Linux)
238-
class POSIXSampleRunner: SampleRunner {
239-
func run(_ name: String, fn: (Int) -> Void, num_iters: UInt) -> UInt64 {
240-
// Start the timer.
241-
#if SWIFT_RUNTIME_ENABLE_LEAK_CHECKER
242-
var str = name
243-
startTrackingObjects(UnsafeMutableRawPointer(str._core.startASCII))
244-
#endif
245-
var start_ticks = timespec(tv_sec: 0, tv_nsec: 0)
246-
clock_gettime(CLOCK_REALTIME, &start_ticks)
247-
fn(Int(num_iters))
248-
// Stop the timer.
249-
var end_ticks = timespec(tv_sec: 0, tv_nsec: 0)
250-
clock_gettime(CLOCK_REALTIME, &end_ticks)
251-
#if SWIFT_RUNTIME_ENABLE_LEAK_CHECKER
252-
stopTrackingObjects(UnsafeMutableRawPointer(str._core.startASCII))
253-
#endif
254-
255-
// Compute the spent time and the scaling factor.
234+
class Timer {
235+
typealias TimeT = timespec
236+
func getTime() -> TimeT {
237+
var ticks = timespec(tv_sec: 0, tv_nsec: 0)
238+
clock_gettime(CLOCK_REALTIME, &ticks)
239+
return ticks
240+
}
241+
func diffTimeInNanoSeconds(from start_ticks: TimeT, to end_ticks: TimeT) -> UInt64 {
256242
var elapsed_ticks = timespec(tv_sec: 0, tv_nsec: 0)
257243
if end_ticks.tv_nsec - start_ticks.tv_nsec < 0 {
258244
elapsed_ticks.tv_sec = end_ticks.tv_sec - start_ticks.tv_sec - 1
@@ -265,31 +251,42 @@ class POSIXSampleRunner: SampleRunner {
265251
}
266252
}
267253
#else
268-
class DarwinSampleRunner: SampleRunner {
254+
class Timer {
255+
typealias TimeT = UInt64
269256
var info = mach_timebase_info_data_t(numer: 0, denom: 0)
270257
init() {
271258
mach_timebase_info(&info)
272259
}
260+
func getTime() -> TimeT {
261+
return mach_absolute_time()
262+
}
263+
func diffTimeInNanoSeconds(from start_ticks: TimeT, to end_ticks: TimeT) -> UInt64 {
264+
let elapsed_ticks = end_ticks - start_ticks
265+
return elapsed_ticks * UInt64(info.numer) / UInt64(info.denom)
266+
}
267+
}
268+
#endif
269+
270+
class SampleRunner {
271+
let timer = Timer()
273272
func run(_ name: String, fn: (Int) -> Void, num_iters: UInt) -> UInt64 {
274273
// Start the timer.
275274
#if SWIFT_RUNTIME_ENABLE_LEAK_CHECKER
276275
var str = name
277276
startTrackingObjects(UnsafeMutableRawPointer(str._core.startASCII))
278277
#endif
279-
let start_ticks = mach_absolute_time()
278+
let start_ticks = timer.getTime()
280279
fn(Int(num_iters))
281280
// Stop the timer.
282-
let end_ticks = mach_absolute_time()
281+
let end_ticks = timer.getTime()
283282
#if SWIFT_RUNTIME_ENABLE_LEAK_CHECKER
284283
stopTrackingObjects(UnsafeMutableRawPointer(str._core.startASCII))
285284
#endif
286285

287286
// Compute the spent time and the scaling factor.
288-
let elapsed_ticks = end_ticks - start_ticks
289-
return elapsed_ticks * UInt64(info.numer) / UInt64(info.denom)
287+
return timer.diffTimeInNanoSeconds(from: start_ticks, to: end_ticks)
290288
}
291289
}
292-
#endif
293290

294291
/// Invoke the benchmark entry point and return the run time in milliseconds.
295292
func runBench(_ name: String, _ fn: (Int) -> Void, _ c: TestConfig) -> BenchResults {
@@ -300,11 +297,7 @@ func runBench(_ name: String, _ fn: (Int) -> Void, _ c: TestConfig) -> BenchResu
300297
print("Running \(name) for \(c.numSamples) samples.")
301298
}
302299

303-
#if os(Linux)
304-
let sampler:SampleRunner = POSIXSampleRunner()
305-
#else
306-
let sampler:SampleRunner = DarwinSampleRunner()
307-
#endif
300+
let sampler = SampleRunner()
308301
for s in 0..<c.numSamples {
309302
let time_per_sample: UInt64 = 1_000_000_000 * UInt64(c.iterationScale)
310303

0 commit comments

Comments
 (0)