@@ -230,29 +230,15 @@ func stopTrackingObjects(_: UnsafeMutableRawPointer) -> Int
230
230
231
231
#endif
232
232
233
- protocol SampleRunner {
234
- func run( _ name: String , fn: ( Int ) -> Void , num_iters: UInt ) -> UInt64
235
- }
236
-
237
233
#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 {
256
242
var elapsed_ticks = timespec ( tv_sec: 0 , tv_nsec: 0 )
257
243
if end_ticks. tv_nsec - start_ticks. tv_nsec < 0 {
258
244
elapsed_ticks. tv_sec = end_ticks. tv_sec - start_ticks. tv_sec - 1
@@ -265,31 +251,42 @@ class POSIXSampleRunner: SampleRunner {
265
251
}
266
252
}
267
253
#else
268
- class DarwinSampleRunner : SampleRunner {
254
+ class Timer {
255
+ typealias TimeT = UInt64
269
256
var info = mach_timebase_info_data_t ( numer: 0 , denom: 0 )
270
257
init ( ) {
271
258
mach_timebase_info ( & info)
272
259
}
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 ( )
273
272
func run( _ name: String , fn: ( Int ) -> Void , num_iters: UInt ) -> UInt64 {
274
273
// Start the timer.
275
274
#if SWIFT_RUNTIME_ENABLE_LEAK_CHECKER
276
275
var str = name
277
276
startTrackingObjects ( UnsafeMutableRawPointer ( str. _core. startASCII) )
278
277
#endif
279
- let start_ticks = mach_absolute_time ( )
278
+ let start_ticks = timer . getTime ( )
280
279
fn ( Int ( num_iters) )
281
280
// Stop the timer.
282
- let end_ticks = mach_absolute_time ( )
281
+ let end_ticks = timer . getTime ( )
283
282
#if SWIFT_RUNTIME_ENABLE_LEAK_CHECKER
284
283
stopTrackingObjects ( UnsafeMutableRawPointer ( str. _core. startASCII) )
285
284
#endif
286
285
287
286
// 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)
290
288
}
291
289
}
292
- #endif
293
290
294
291
/// Invoke the benchmark entry point and return the run time in milliseconds.
295
292
func runBench( _ name: String , _ fn: ( Int ) -> Void , _ c: TestConfig ) -> BenchResults {
@@ -300,11 +297,7 @@ func runBench(_ name: String, _ fn: (Int) -> Void, _ c: TestConfig) -> BenchResu
300
297
print ( " Running \( name) for \( c. numSamples) samples. " )
301
298
}
302
299
303
- #if os(Linux)
304
- let sampler : SampleRunner = POSIXSampleRunner ( )
305
- #else
306
- let sampler : SampleRunner = DarwinSampleRunner ( )
307
- #endif
300
+ let sampler = SampleRunner ( )
308
301
for s in 0 ..< c. numSamples {
309
302
let time_per_sample : UInt64 = 1_000_000_000 * UInt64( c. iterationScale)
310
303
0 commit comments