running 3 tests
test bench_counter ... bench: 15,851,930.00 ns/iter (+/- 1,636,962.00)
test bench_naive ... bench: 15,800,950.00 ns/iter (+/- 1,695,916.00)
test bench_rayon ... bench: 68,320,460.00 ns/iter (+/- 21,150,254.00)
Created
December 6, 2024 01:17
-
-
Save lxl66566/e6e27ed3913c2f9248b427a39432d45c to your computer and use it in GitHub Desktop.
Rust counter implemention benchmark
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![feature(test)] | |
extern crate test; | |
use std::collections::HashMap; | |
use counter::Counter; | |
use dashmap::DashMap; | |
use rayon::prelude::*; | |
use test::black_box; | |
fn main() {} | |
fn long_string() -> String { | |
let mut s = String::new(); | |
for _ in 0..100000 { | |
for j in 'a'..='z' { | |
s.push(j); | |
} | |
} | |
s | |
} | |
#[bench] | |
fn bench_naive(b: &mut test::Bencher) { | |
let s = long_string(); | |
b.iter(|| { | |
let mut map = HashMap::new(); | |
for c in s.chars() { | |
*map.entry(c).or_insert(0) += 1; | |
} | |
black_box(map); | |
}); | |
} | |
#[bench] | |
fn bench_counter(b: &mut test::Bencher) { | |
let s = long_string(); | |
b.iter(|| { | |
let map = s.chars().collect::<Counter<_>>(); | |
black_box(map); | |
}); | |
} | |
#[bench] | |
fn bench_rayon(b: &mut test::Bencher) { | |
let s = long_string(); | |
b.iter(|| { | |
let map = DashMap::new(); | |
s.par_chars().for_each(|c| { | |
*map.entry(c).or_insert(0) += 1; | |
}); | |
black_box(map); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment