Skip to content

Commit bd43d54

Browse files
committed
benchmarks: Move the setup and teardown functions out of the sample loop.
This is important to minimize the runtime when many samples are taken.
1 parent 50db6f1 commit bd43d54

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

benchmark/utils/DriverUtils.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,9 @@ func runBench(_ test: BenchmarkInfo, _ c: TestConfig) -> BenchResults? {
341341
}
342342

343343
let sampler = SampleRunner(c)
344+
test.setUpFunction?()
345+
344346
for s in 0..<c.numSamples {
345-
test.setUpFunction?()
346347
let nsPerSecond = 1_000_000_000.0 // nanoseconds
347348
let time_per_sample = UInt64(c.sampleTime * nsPerSecond)
348349

@@ -385,8 +386,8 @@ func runBench(_ test: BenchmarkInfo, _ c: TestConfig) -> BenchResults? {
385386
if c.verbose {
386387
print(" Sample \(s),\(samples[s])")
387388
}
388-
test.tearDownFunction?()
389389
}
390+
test.tearDownFunction?()
390391

391392
let (mean, sd) = internalMeanSD(samples)
392393

benchmark/utils/TestsUtils.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,7 @@ public struct BenchmarkInfo {
143143
/// Shadow static variable for computed property tearDownFunction.
144144
private var _tearDownFunction: (() -> ())?
145145

146-
/// An optional function that if non-null is run immediately after a sample is
147-
/// taken.
146+
/// An optional function that if non-null is run after samples are taken.
148147
public var tearDownFunction: (() -> ())? {
149148
if !shouldRun {
150149
return nil

0 commit comments

Comments
 (0)