[][src]Crate benchmarking

Benchmarking

This crate can be used to execute something and measure the execution time. It does not output anything to screens and filesystems.

Examples

extern crate benchmarking;

fn main() {
    const VEC_LENGTH: usize = 100;

    benchmarking::warm_up();

    let bench_result = benchmarking::measure_function(|measurer| {
        let mut vec: Vec<usize> = Vec::with_capacity(VEC_LENGTH);

        measurer.measure(|| {
            for i in 0..VEC_LENGTH {
                vec.push(i);
            }
        });

        /*
            // Start the measurement
            for i in 0..VEC_LENGTH {
                vec.push(i);
            }
            // End the measurement
        */
    }).unwrap();

    println!("Filling 0 to 99 into a vec takes {:?}!", bench_result.elapsed());
}
extern crate benchmarking;

fn main() {
    const VEC_LENGTH: usize = 100;

    benchmarking::warm_up();

    let bench_result = benchmarking::measure_function(|measurer| {
        let mut vec: Vec<usize> = Vec::with_capacity(VEC_LENGTH);

        measurer.measure_for_loop(0..VEC_LENGTH, |_, loop_seq| {
            vec.push(loop_seq);
        });

        /*
            for i in 0...VEC_LENGTH {
                // Start the measurement
                vec.push(i);
                // End the measurement
            }
        */
    }).unwrap();

    println!("Pushing a number into a vec takes {:?}!", bench_result.elapsed());
}
extern crate benchmarking;

fn main() {
    const VEC_LENGTH: usize = 100;

    benchmarking::warm_up();

    let bench_result = benchmarking::measure_function(|measurer| {
        let mut vec: Vec<usize> = Vec::with_capacity(VEC_LENGTH);

        measurer.measure_while_loop(|loop_seq| {
            loop_seq < VEC_LENGTH
        }, |loop_seq| {
            vec.push(loop_seq);
        });

        /*
            let mut i = 0;

            while i < VEC_LENGTH {
                // Start the measurement
                vec.push(i);
                // End the measurement

                i += 1;
            }
        */
    }).unwrap();

    println!("Pushing a number into a vec takes {:?}!", bench_result.elapsed());
}
  • The warm_up and warm_up_with_duration functions of the benchmarking crate runs on one thread. To warm up all CPUs, you can use the warm_up_multi_thread and warm_up_multi_thread_with_duration functions instead.
  • The measure_function and measure_function_with_times functions of the benchmarking crate can execute a closure for N times. To execute it repeatly for a while instead, you can use the bench_function and bench_function_with_duration functions.
  • To execute a closure with multiple threads to measure the throughput, you can use the multi_thread_bench_function and multi_thread_bench_function_with_duration functions of the benchmarking crate.

Structs

MeasureResult

The result of measurement.

Measurer

To measure the execution time.

Enums

BenchmarkError
MeasureLoopResult

To control whether to continue running the loop.

Functions

bench_function

Run a function for 5 seconds and measure its execution time.

bench_function_with_duration

Run a function with a specific duration and measure its execution time.

measure_function

Run a function 10 times and measure its execution time.

measure_function_with_times

Run a function with a specific times and measure its execution time.

multi_thread_bench_function

Run a function with a number of threads for 5 seconds and measure its execution time.

multi_thread_bench_function_with_duration

Run a function with a number of threads and a specific duration and measure its execution time.

warm_up

To stimulate CPU to wake up. The running duration is 3 seconds.

warm_up_multi_thread

To stimulate CPUs to wake up.

warm_up_multi_thread_with_duration

To stimulate CPUs to wake up. The running duration is 3 seconds.

warm_up_with_duration

To stimulate CPU to wake up.