Skip to content

Commit 894c9ca

Browse files
committed
Add benchmark for VecDeque append
1 parent 6ebd62b commit 894c9ca

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

src/liballoc/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,8 @@ path = "../liballoc/tests/lib.rs"
2323
[[bench]]
2424
name = "collectionsbenches"
2525
path = "../liballoc/benches/lib.rs"
26+
27+
[[bench]]
28+
name = "vec_deque_append_bench"
29+
path = "../liballoc/benches/vec_deque_append.rs"
30+
harness = false
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(duration_as_u128)]
12+
use std::{collections::VecDeque, time::Instant};
13+
14+
const VECDEQUE_LEN: i32 = 100000;
15+
const WARMUP_N: usize = 100;
16+
const BENCH_N: usize = 1000;
17+
18+
fn main() {
19+
let a: VecDeque<i32> = (0..VECDEQUE_LEN).collect();
20+
let b: VecDeque<i32> = (0..VECDEQUE_LEN).collect();
21+
22+
for _ in 0..WARMUP_N {
23+
let mut c = a.clone();
24+
let mut d = b.clone();
25+
c.append(&mut d);
26+
}
27+
28+
let mut durations = Vec::with_capacity(BENCH_N);
29+
30+
for _ in 0..BENCH_N {
31+
let mut c = a.clone();
32+
let mut d = b.clone();
33+
let before = Instant::now();
34+
c.append(&mut d);
35+
let after = Instant::now();
36+
durations.push(after.duration_since(before));
37+
}
38+
39+
let l = durations.len();
40+
durations.sort();
41+
42+
assert!(BENCH_N % 2 == 0);
43+
let median = (durations[(l / 2) - 1] + durations[l / 2]) / 2;
44+
println!(
45+
"\ncustom-bench vec_deque_append {:?} ns/iter\n",
46+
median.as_nanos()
47+
);
48+
}

0 commit comments

Comments
 (0)