1 unstable release

0.1.0 Nov 28, 2023

#204 in Profiling

MIT license

24KB
508 lines

Chairmark

A benchmaking library in rust.

quick start

use chairmark::{chair, agg_and_cmp};

// custom function for sorting data
fn bubblesort(data: &mut Vec<u32>) {
    /* your fancy sorting algorithm */
}

fn main() {
    const RUNS: usize = 1_000;
    let prepare = |_| (0u32..1_000).collect();
    let bubblesort = chair_prepare(RUNS, prepare, |mut data| bubblesort(&mut data));
    let std = chair_prepare(RUNS, prepare, |mut data| data.sort());

    let compare = agg_and_cmp![std, bubblesort];
    println!("{}", compare);
}

TODOs

  • add more examples in README

lib.rs:

Crate for benchmarking

Unfortunately, there's no stable support for proper cargo bench stuff. Therefore, I made this crate

The idea is to have a function that benchmarks another given function. This will be done a specified number of times for more reliable data. During this benchmark ("chairmark") running time data is collected that can be aggregated etc..

This data can then be displayed easliy and readable. There's also an additional feature for comparing different chairmarks. This can be used to compare a custom implementation with one from the standard library or different implmenentations.

Examples

example without using macros

use chairmark::{chair, Time, Comparison};

// checks if number is power of two
fn is_power(arg: u32) -> bool {
    arg.to_ne_bytes().into_iter().sum::<u8>() == 1
}

fn main() {
    const NUMBER: u32 = 69;

    // benchmark std function
    let std = chair(1_000, || NUMBER.is_power_of_two()).aggregate::<Time>();
    // benchmark custom function
    let custom = chair(1_000, || is_power(NUMBER)).aggregate::<Time>();

    // compare
    let compare = Comparison::from([("std", std), ("custom", custom)]);

    // display as a table
    println!("{}", compare);
}

example with all macros

use chairmark::*;

// checks if number is power of two
fn is_power(arg: u32) -> bool {
    arg.to_ne_bytes().into_iter().sum::<u8>() == 1
}

fn main() {
    const NUMBER: u32 = 69;

    // benchmark std function
    let std = chair(1_000, || NUMBER.is_power_of_two());
    // benchmark custom function
    let custom = chair(1_000, || is_power(NUMBER));

    // compare
    let compare = agg_and_cmp![std, custom];

    // display as table
    println!("{}", compare);
}

example with prepared data

use chairmark::*;

// custom sort function
fn bubblesort(data: &mut Vec<u32>) {
    /* your great bubblesort implementation */
}

fn main() {
    let prepare = |idx| (0..idx).map(|x| x as u32 / 3).collect::<Vec<_>>();

    // benchmark std functions
    let std_stable = chair_prepare(1_000, prepare, |mut data| data.sort());
    let std_unstable = chair_prepare(1_000, prepare, |mut data| data.sort_unstable());
    // benchmark custom function
    let custom = chair_prepare(1_000, prepare, |mut data| bubblesort(&mut data));

    // aggregate and compare
    let compare = agg_and_cmp![std_stable, std_unstable, custom];

    println!("{}", compare);
}

Dependencies