function_benchmarker

Crates.iofunction_benchmarker
lib.rsfunction_benchmarker
version0.1.2
sourcesrc
created_at2024-09-15 03:40:12.247346
updated_at2024-09-15 04:35:00.92283
descriptionA proc macro for benchmarking Rust code
homepage
repositoryhttps://github.com/learnwithsobhit/benchmarker
max_upload_size
id1375214
size6,897
(learnwithsobhit)

documentation

https://docs.rs/benchmarker

README

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.

Commit count: 0

cargo fmt