Skip to content

Commit 295b6f4

Browse files
palimondoDavide Italiano
authored andcommitted
[benchmark] Refactor to currency type Int
Removed unnecessary use of UInt64, where appropriate, following the advice from Swift Language Guide: > Use the `Int` type for all general-purpose integer constants and variables in your code, even if they’re known to be nonnegative. Using the default integer type in everyday situations means that integer constants and variables are immediately interoperable in your code and will match the inferred type for integer literal values. https://docs.swift.org/swift-book/LanguageGuide/TheBasics.html#ID324
1 parent 658e0f3 commit 295b6f4

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

benchmark/utils/DriverUtils.swift

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import LibProc
2020
import TestsUtils
2121

2222
struct BenchResults {
23-
typealias T = UInt64
23+
typealias T = Int
2424
private let samples: [T]
2525
let maxRSS: Int
2626
let stats: Stats
@@ -37,11 +37,11 @@ struct BenchResults {
3737
return samples[index]
3838
}
3939

40-
var sampleCount: T { return UInt64(samples.count) }
40+
var sampleCount: T { return samples.count }
4141
var min: T { return samples.first! }
4242
var max: T { return samples.last! }
43-
var mean: T { return UInt64(stats.mean.rounded()) }
44-
var sd: T { return UInt64(stats.standardDeviation.rounded()) }
43+
var mean: T { return Int(stats.mean.rounded()) }
44+
var sd: T { return Int(stats.standardDeviation.rounded()) }
4545
var median: T { return self[0.5] }
4646
}
4747

@@ -235,7 +235,7 @@ struct Stats {
235235
var variance: Double { return n < 2 ? 0.0 : S / Double(n - 1) }
236236
var standardDeviation: Double { return variance.squareRoot() }
237237

238-
static func collect(_ s: inout Stats, _ x: UInt64){
238+
static func collect(_ s: inout Stats, _ x: Int){
239239
Stats.runningMeanVariance(&s, Double(x))
240240
}
241241

@@ -387,7 +387,7 @@ final class SampleRunner {
387387
}
388388

389389
/// Measure the `fn` and return the average sample time per iteration (μs).
390-
func measure(_ name: String, fn: (Int) -> Void, numIters: Int) -> UInt64 {
390+
func measure(_ name: String, fn: (Int) -> Void, numIters: Int) -> Int {
391391
#if SWIFT_RUNTIME_ENABLE_LEAK_CHECKER
392392
name.withCString { p in startTrackingObjects(p) }
393393
#endif
@@ -400,13 +400,14 @@ final class SampleRunner {
400400
name.withCString { p in stopTrackingObjects(p) }
401401
#endif
402402

403-
return lastSampleTime / UInt64(numIters) / 1000
403+
// Convert to μs and compute the average sample time per iteration.
404+
return Int(lastSampleTime / 1000) / numIters
404405
}
405406
}
406407

407408
/// Invoke the benchmark entry point and return the run time in milliseconds.
408409
func runBench(_ test: BenchmarkInfo, _ c: TestConfig) -> BenchResults? {
409-
var samples = [UInt64](repeating: 0, count: c.numSamples)
410+
var samples = [Int](repeating: 0, count: c.numSamples)
410411

411412
// Before we do anything, check that we actually have a function to
412413
// run. If we don't it is because the benchmark is not supported on
@@ -427,15 +428,15 @@ func runBench(_ test: BenchmarkInfo, _ c: TestConfig) -> BenchResults? {
427428

428429
for s in 0..<c.numSamples {
429430
var scale : Int
430-
var elapsed_time : UInt64 = 0
431+
var elapsed_time : Int = 0
431432
if c.fixedNumIters == 0 {
432433
elapsed_time = sampler.measure(test.name, fn: testFn, numIters: 1)
433434

434435
if elapsed_time > 0 {
435436
let usPerSecond = 1_000_000.0 // microseconds (μs)
436-
let timePerSample = UInt64(c.sampleTime * usPerSecond)
437+
let timePerSample = Int(c.sampleTime * usPerSecond)
437438
/// Number of iterations to make `testFn` run for the desired time.
438-
scale = Int(timePerSample / elapsed_time)
439+
scale = timePerSample / elapsed_time
439440
} else {
440441
if c.verbose {
441442
print(" Warning: elapsed time is 0. This can be safely ignored if the body is empty.")

0 commit comments

Comments
 (0)