Crates.io | function_benchmarker |
lib.rs | function_benchmarker |
version | 0.1.2 |
source | src |
created_at | 2024-09-15 03:40:12.247346 |
updated_at | 2024-09-15 04:35:00.92283 |
description | A proc macro for benchmarking Rust code |
homepage | |
repository | https://github.com/learnwithsobhit/benchmarker |
max_upload_size | |
id | 1375214 |
size | 6,897 |
A simple and efficient benchmarking tool for Rust functions.
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.
This project is licensed under the MIT License. See the LICENSE
file for more details.