Comments (5)
Hi,
There's nothing built in for that, but there are a couple of things you could try:
-
Generate two benchmarks runs (just parameterize the benchmark) and use start/stop measuring to get eg. A/B measurements - possibly ok if you runtime isn't too bad - statistically it'll give you something useful. Then export this in one of the supported formats and calculate the ratio
-
You could also try with custom measurements using the BenchmarkClock.now and register custom metrics for A/B/ratio as you suggest - that's probably cleaner and what I'd try first. Then you measure the time yourself basically.
from package-benchmark.
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
from package-benchmark.
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)
}
}
}
from package-benchmark.
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
from package-benchmark.
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.
from package-benchmark.
Related Issues (20)
- Seven entries per benchmark per metric in influx. HOT 3
- Benchmark plugin swallows error codes from benchmark crashes HOT 3
- Could not build Objective-C module 'JWTKit' HOT 2
- Improved Docs for iOS Projects? HOT 6
- Error enum values not propagated as exit codes for benchmark comparison in CI. HOT 5
- Differing scaling factors across the same benchmark misrepresented in comaprisons. HOT 3
- Can engineers force memory reporting into one consistent scale unit (K or M)? HOT 2
- Any API for specifying `minIterations` on a benchmark? HOT 2
- Incorrect CI PR benchmark comparison HOT 3
- Support for memcpy metrics HOT 5
- Add more stats from proc_pid_rusage
- Can `--format markdown` produce legit markdown tables? HOT 14
- Can I pass additional flags when building benchmark? HOT 4
- Make wall clock time and cpu time spent closer HOT 1
- Perform overhead test run and "clean" measurement data
- Allow passing of state between startup/shutdown code and the actual benchmark inside the closure
- Add examples and documentation for regex
- Measure Swift exclusivity checking overhead HOT 4
- Warning about visionOS HOT 3
- Swift 6 language mode support needed
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from package-benchmark.