diff options
| author | bors <bors@rust-lang.org> | 2018-08-18 08:56:12 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2018-08-18 08:56:12 +0000 |
| commit | d5b6b95aef94169b5dbe4dbb1357d4bab1fc9800 (patch) | |
| tree | 3e481559470fb4d3e90f4a5f5b59067fbd3c1208 /src/liballoc/benches | |
| parent | 6b1ff19af36f7bbf1974579ec1b9bf2c8ccd595e (diff) | |
| parent | b063bd4616bf2f27b814f39f0e452efd171fd539 (diff) | |
Auto merge of #52553 - Pazzaz:vecdeque-append, r=SimonSapin
Non-naive implementation of `VecDeque.append` Replaces the old, simple implementation with a more manual (and **unsafe** 😱) one. I've added 1 more test and verified that it covers all 6 code paths in the function. This new implementation was about 60% faster than the old naive one when I tried benchmarking it.
Diffstat (limited to 'src/liballoc/benches')
| -rw-r--r-- | src/liballoc/benches/vec_deque_append.rs | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/liballoc/benches/vec_deque_append.rs b/src/liballoc/benches/vec_deque_append.rs new file mode 100644 index 00000000000..bd335651137 --- /dev/null +++ b/src/liballoc/benches/vec_deque_append.rs @@ -0,0 +1,48 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(duration_as_u128)] +use std::{collections::VecDeque, time::Instant}; + +const VECDEQUE_LEN: i32 = 100000; +const WARMUP_N: usize = 100; +const BENCH_N: usize = 1000; + +fn main() { + let a: VecDeque<i32> = (0..VECDEQUE_LEN).collect(); + let b: VecDeque<i32> = (0..VECDEQUE_LEN).collect(); + + for _ in 0..WARMUP_N { + let mut c = a.clone(); + let mut d = b.clone(); + c.append(&mut d); + } + + let mut durations = Vec::with_capacity(BENCH_N); + + for _ in 0..BENCH_N { + let mut c = a.clone(); + let mut d = b.clone(); + let before = Instant::now(); + c.append(&mut d); + let after = Instant::now(); + durations.push(after.duration_since(before)); + } + + let l = durations.len(); + durations.sort(); + + assert!(BENCH_N % 2 == 0); + let median = (durations[(l / 2) - 1] + durations[l / 2]) / 2; + println!( + "\ncustom-bench vec_deque_append {:?} ns/iter\n", + median.as_nanos() + ); +} |
