#benchmark #performance #proc-macro #execution-time #testing

macro function_benchmarker

A proc macro for benchmarking Rust code

3 releases

0.1.2 Sep 15, 2024
0.1.1 Sep 15, 2024
0.1.0 Sep 15, 2024

#222 in Profiling

37 downloads per month

MIT/Apache

6KB

benchmarker

A simple and efficient benchmarking tool for Rust functions.

Crates.io Documentation License: MIT

Features

  • Easy-to-use macro for benchmarking functions
  • Measures execution time and calculates statistics
  • Supports multiple iterations for accurate results
  • Customizable number of warmup rounds

Usage

Add this to your Cargo.toml:

[dependencies]
chrono = "0.4.38"
jemalloc-ctl = "0.5.4"
jemallocator = "0.5.4"
benchmarker = "*"

Then, you can use the bench macro to benchmark your functions:


#[global_allocator]
static GLOBAL: jemallocator::Jemalloc = jemallocator::Jemalloc;
use function_benchmarker::benchmark;


#[benchmark]
pub fn test_benchmark(limit: u64) -> Vec<u64> {
    // Function to calculate prime numbers efficiently
    if limit <= 1 {
        return vec![];
    }

    let mut is_prime = vec![true; limit as usize + 1];
    is_prime[0] = false;
    is_prime[1] = false;

    let sqrt_limit = (limit as f64).sqrt() as u64;
    for i in 2..=sqrt_limit {
        if is_prime[i as usize] {
            let mut j = i * i;
            while j <= limit {
                is_prime[j as usize] = false;
                j += i;
            }
        }
    }

    (2..=limit).filter(|&i| is_prime[i as usize]).collect()
}

fn main() {
    // Benchmark the prime calculation function
    let limit: u64 = 1_000_000;
    let primes = test_benchmark(limit);
    println!("Number of primes up to {}: {}", limit, primes.len());
}

output:
Entering function: test_benchmark
Exiting function: test_benchmark (took 61 ms) memory used: 1732224 bytes
Number of primes up to 1000000: 78498

This will print the average time taken to execute the function and memory statistics.

License

This project is licensed under the MIT License. See the LICENSE file for more details.

Dependencies

~220–660KB
~16K SLoC