benched

A statistics-based benchmarking tool for Haxe, inspired by criterion
https://github.com/hamaluik/benched

To install, run:

haxelib install benched 0.2.0 

See using Haxelib in Haxelib documentation for more information.

README.md

Benched

A statistics-based benchmarking tool for Haxe, inspired by criterion.

API

API documentation is available.

Sample

A sample is provided in samples/Fib.hx. We start by calculating the Fibonacii sequence for any given number using recursion, and benchmark the results. Note that by default, we will collect 50 samples which take at least 0.5s each to collect, meaning each benchmark will take at least 25s to run. Given that we're running 3 benchmarks, each time we run this it will take about a minute and a half—be patient!.

import haxe.Serializer;
import benched.Benched;

class Fib {
    static function fibonacci(n: Int): Int {
        return switch n {
            case 0: 1;
            case 1: 1;
            case n: fibonacci(n - 1) + fibonacci(n - 2);
        }
    }

    public static function main() {
        var bencher = new Benched();
        bencher.benchmark("Fibonacci(1)", () -> fibonacci(1));
        bencher.benchmark("Fibonacci(5)", () -> fibonacci(5));
        bencher.benchmark("Fibonacci(10)", () -> fibonacci(10));
        Sys.println('### Naive Implementation');
        Sys.println(bencher.generateReport());

        // save the results for later
        var s = new Serializer();
        s.serialize(bencher);
        sys.io.File.saveContent("_fib_naive.hxs", s.toString());
    }
}

This generates the following table:

Naive Implementation

BenchmarkMean Time / Iteration
Fibonacci(1)826.579 [ps] ± 102.629 [ps]
Fibonacci(5) 66.082 [ns] ± 1.739 [ns]
Fibonacci(10)774.688 [ns] ± 10.013 [ns]

We notice this takes a bit of time, so we re-write the fibonacci function to not be recursive and hopefully faster:

import haxe.Unserializer;
import haxe.Serializer;
import benched.Benched;

class Fib {
    static function fibonacci(n: Int): Int {
        var a: Int = 0;
        var b: Int = 1;

        return switch(n) {
            case 0: b;
            case _: {
                for(_ in 0...n) {
                    var c = a + b;
                    a = b;
                    b = c;
                }
                b;
            }
        }
    }

    public static function main() {
        // now we've made some changes to our fibonacci calculator
        // benchmark the results
        var bencher = new Benched();
        bencher.benchmark("Fibonacci(1)", () -> fibonacci_naive(1));
        bencher.benchmark("Fibonacci(5)", () -> fibonacci_naive(5));
        bencher.benchmark("Fibonacci(10)", () -> fibonacci_naive(10));
        Sys.println('### Optimized Implementation');
        Sys.println(bencher.generateReport());

        // now load our old results and see if we made things faster
        var oldBencher: Benched = new Unserializer(sys.io.File.getContent("_fib_naive.hxs")).unserialize();
        Sys.println("### Changes");
        Sys.println(bencher.generateComparisonReport(oldBencher));
    }
}

This results in the following tables, showing that we've sped things up considerably!

Optimized Implementation

BenchmarkMean Time / Iteration
Fibonacci(1) 5.347 [ns] ± 431.962 [ps]
Fibonacci(5) 8.852 [ns] ± 307.325 [ps]
Fibonacci(10) 13.794 [ns] ± 609.535 [ps]

Changes

BenchmarkMean Time / IterationOld Mean Time / IterationChangePerformance Difference
Fibonacci(1) 5.347 [ns] ± 431.962 [ps]826.579 [ps] ± 102.629 [ps]~6.5× Slower+546.9%
Fibonacci(5) 8.852 [ns] ± 307.325 [ps] 66.082 [ns] ± 1.739 [ns]~7.5× Faster-86.6%
Fibonacci(10) 13.794 [ns] ± 609.535 [ps]774.688 [ns] ± 10.013 [ns]~56.2× Faster-98.2%
Contributors
hamaluik
Version
0.2.0
Published
5 years ago
License
Apache

All libraries are free

Every month, more than a thousand developers use Haxelib to find, share, and reuse code — and assemble it in powerful new ways. Enjoy Haxe; It is great!

Explore Haxe

Haxe Manual

Haxe Code Cookbook

Haxe API documentation

You can try Haxe in the browser! try.haxe.org

Join us on GitHub!

Haxe is being developed on GitHub. Feel free to contribute or report issues to our projects.

Haxe on GitHub