-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ratio between two related operations #226
Comments
Hi, There's nothing built in for that, but there are a couple of things you could try:
|
I've taken the second approach and have the following extension on Benchmark now: extension Benchmark {
@discardableResult
convenience init?(_ name: String, forward: @escaping (Benchmark) -> (), reverse: @escaping (Benchmark) -> ()) {
self.init(name, configuration: .init(metrics: [CustomMeasurement.forward, CustomMeasurement.reverse, CustomMeasurement.ratio])) { benchmark in
let startForward = BenchmarkClock.now
forward(benchmark)
let endForward = BenchmarkClock.now
let startReverse = BenchmarkClock.now
reverse(benchmark)
let endReverse = BenchmarkClock.now
let forward = Int((endForward - startForward).nanoseconds())
let reverse = Int((endReverse - startReverse).nanoseconds())
benchmark.measurement(CustomMeasurement.forward, forward)
benchmark.measurement(CustomMeasurement.reverse, reverse)
benchmark.measurement(CustomMeasurement.ratio, reverse / forward)
}
}
} Only downside here is that we can only measure integer values so can't turn the ratio into a display of factors of say 1.5 etc. Any ideas and/or tips on that? (or the implementation itself) Edit: removed the scaled iterations loop around the forward, reverse closure calls from the implementation since that was optimising out the entire closure |
How about keeping microseconds for forward/reverse, but nanoseconds for the ratio? Need to tweak the runtime of the measurement such that microseconds (or more) are suitable though. (On mobile device, but seems ChatGPT helped format reasonably) Something like: extension Benchmark {
@discardableResult
convenience init?(_ name: String, forward: @escaping (Benchmark) -> (), reverse: @escaping (Benchmark) -> ()) {
self.init(name, configuration: .init(metrics: [CustomMeasurement.forward, CustomMeasurement.reverse, CustomMeasurement.ratio])) { benchmark in
let startForward = BenchmarkClock.now
forward(benchmark)
let endForward = BenchmarkClock.now
let startReverse = BenchmarkClock.now
reverse(benchmark)
let endReverse = BenchmarkClock.now
// Calculate times in nanoseconds
let forwardTimeNanos = (endForward - startForward).nanoseconds()
let reverseTimeNanos = (endReverse - startReverse).nanoseconds()
// Convert times to microseconds for storage
let forwardTimeMicros = forwardTimeNanos / 1000
let reverseTimeMicros = reverseTimeNanos / 1000
// Ensure forwardTimeNanos is not zero to avoid division by zero
guard forwardTimeNanos != 0 else {
print("Forward time in nanoseconds is zero, cannot compute ratio.")
return
}
// Calculate ratio using nanosecond precision
let ratio = reverseTimeNanos / forwardTimeNanos
benchmark.measurement(CustomMeasurement.forward, forwardTimeMicros)
benchmark.measurement(CustomMeasurement.reverse, reverseTimeMicros)
benchmark.measurement(CustomMeasurement.ratio, ratio)
}
}
} |
That still doesn't allow to display a ratio of 1.5 right? So in terms of precision if execution time is 2,000,000 ns vs 3,000,000 the ratio will still be 1.5 regardless of precision |
Right, only integer samples - but in that case you would store 2,000(us) vs 3,000(us) with 1,500,000ns for your custom measurements, so you'd see the fractional part in that case. Another approach is to just export the raw data in one of the output formats and do your own post processing for that specific case. |
Hey there! I was hoping to use the package to measure the wall clock time of two related operations in a single benchmark. So that I can report on both their execution times and also return the ratio between them as a result of that single benchmark.
What is the best way to go around this? I know I can make custom metrics but I'm not sure if it's possible to do two time measurements within one benchmark?
The text was updated successfully, but these errors were encountered: