|
| 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