Code Monkey home page Code Monkey logo

Comments (5)

hassila avatar hassila commented on July 21, 2024

Hi,

There's nothing built in for that, but there are a couple of things you could try:

  1. 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

  2. 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.

JaapWijnen avatar JaapWijnen commented on July 21, 2024

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.

hassila avatar hassila commented on July 21, 2024

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.

JaapWijnen avatar JaapWijnen commented on July 21, 2024

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.

hassila avatar hassila commented on July 21, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.