Skip to content

Commit a274f61

Browse files
committed
TEST: Add benchmarks for .sort_by()
1 parent b1b71d6 commit a274f61

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

benches/bench.rs

+80
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ extern crate fnv;
55
#[macro_use]
66
extern crate lazy_static;
77

8+
use std::hash::Hash;
89
use fnv::FnvHasher;
910
use std::hash::BuildHasherDefault;
1011
type FnvBuilder = BuildHasherDefault<FnvHasher>;
@@ -361,13 +362,16 @@ fn lookup_orderedmap_10_000_noexist(b: &mut Bencher) {
361362
// number of items to look up
362363
const LOOKUP_MAP_SIZE: u32 = 100_000_u32;
363364
const LOOKUP_SAMPLE_SIZE: u32 = 5000;
365+
const SORT_MAP_SIZE: usize = 10_000;
364366

365367

368+
// use lazy_static so that comparison benchmarks use the exact same inputs
366369
lazy_static! {
367370
static ref KEYS: Vec<u32> = {
368371
shuffled_keys(0..LOOKUP_MAP_SIZE)
369372
};
370373
}
374+
371375
lazy_static! {
372376
static ref HMAP_100K: HashMap<u32, u32> = {
373377
let c = LOOKUP_MAP_SIZE;
@@ -392,6 +396,25 @@ lazy_static! {
392396
};
393397
}
394398

399+
lazy_static! {
400+
static ref OMAP_SORT_U32: OrderMap<u32, u32> = {
401+
let mut map = OrderMap::with_capacity(SORT_MAP_SIZE);
402+
for &key in &KEYS[..SORT_MAP_SIZE] {
403+
map.insert(key, key);
404+
}
405+
map
406+
};
407+
}
408+
lazy_static! {
409+
static ref OMAP_SORT_S: OrderMap<String, String> = {
410+
let mut map = OrderMap::with_capacity(SORT_MAP_SIZE);
411+
for &key in &KEYS[..SORT_MAP_SIZE] {
412+
map.insert(format!("{:^16x}", &key), String::new());
413+
}
414+
map
415+
};
416+
}
417+
395418
#[bench]
396419
fn lookup_hashmap_100_000_multi(b: &mut Bencher) {
397420
let map = &*HMAP_100K;
@@ -643,3 +666,60 @@ fn many_retain_hashmap_100_000(b: &mut Bencher) {
643666
map
644667
});
645668
}
669+
670+
671+
// simple sort impl for comparison
672+
pub fn simple_sort<K: Ord + Hash, V>(m: &mut OrderMap<K, V>) {
673+
let mut ordered: Vec<_> = m.drain(..).collect();
674+
ordered.sort_by(|left, right| left.0.cmp(&right.0));
675+
m.extend(ordered);
676+
}
677+
678+
679+
#[bench]
680+
fn ordermap_sort_s(b: &mut Bencher) {
681+
let map = OMAP_SORT_S.clone();
682+
683+
// there's a map clone there, but it's still useful to profile this
684+
b.iter(|| {
685+
let mut map = map.clone();
686+
map.sort_keys();
687+
map
688+
});
689+
}
690+
691+
#[bench]
692+
fn ordermap_simple_sort_s(b: &mut Bencher) {
693+
let map = OMAP_SORT_S.clone();
694+
695+
// there's a map clone there, but it's still useful to profile this
696+
b.iter(|| {
697+
let mut map = map.clone();
698+
simple_sort(&mut map);
699+
map
700+
});
701+
}
702+
703+
#[bench]
704+
fn ordermap_sort_u32(b: &mut Bencher) {
705+
let map = OMAP_SORT_U32.clone();
706+
707+
// there's a map clone there, but it's still useful to profile this
708+
b.iter(|| {
709+
let mut map = map.clone();
710+
map.sort_keys();
711+
map
712+
});
713+
}
714+
715+
#[bench]
716+
fn ordermap_simple_sort_u32(b: &mut Bencher) {
717+
let map = OMAP_SORT_U32.clone();
718+
719+
// there's a map clone there, but it's still useful to profile this
720+
b.iter(|| {
721+
let mut map = map.clone();
722+
simple_sort(&mut map);
723+
map
724+
});
725+
}

0 commit comments

Comments
 (0)