Skip to content

Commit

Permalink
Rollup merge of rust-lang#39561 - phungleson:libcollectionsbench, r=a…
Browse files Browse the repository at this point in the history
…lexcrichton

Extract collections benchmarks to libcollections/bench

Good suggestion from @stjepang rust-lang#39484 (comment)

r? @alexcrichton
  • Loading branch information
frewsxcv authored Feb 7, 2017
2 parents cf7fbb7 + 7c8c45e commit 2762b4d
Show file tree
Hide file tree
Showing 31 changed files with 1,442 additions and 1,372 deletions.
7 changes: 3 additions & 4 deletions src/libcollections/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ std_unicode = { path = "../libstd_unicode" }
name = "collectionstest"
path = "../libcollectionstest/lib.rs"

# FIXME: need to extract benchmarks to separate crate
#[[bench]]
#name = "collectionstest"
#path = "../libcollectionstest/lib.rs"
[[bench]]
name = "collectionsbenches"
path = "../libcollections/benches/lib.rs"
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
Expand All @@ -8,13 +8,17 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.


use std::iter::Iterator;
use std::vec::Vec;
use std::collections::BTreeMap;
use std::__rand::{Rng, thread_rng};
use test::{Bencher, black_box};

macro_rules! map_insert_rand_bench {
($name: ident, $n: expr, $map: ident) => (
#[bench]
pub fn $name(b: &mut ::test::Bencher) {
use std::__rand::{thread_rng, Rng};
use test::black_box;

pub fn $name(b: &mut Bencher) {
let n: usize = $n;
let mut map = $map::new();
// setup
Expand All @@ -39,9 +43,7 @@ macro_rules! map_insert_rand_bench {
macro_rules! map_insert_seq_bench {
($name: ident, $n: expr, $map: ident) => (
#[bench]
pub fn $name(b: &mut ::test::Bencher) {
use test::black_box;

pub fn $name(b: &mut Bencher) {
let mut map = $map::new();
let n: usize = $n;
// setup
Expand All @@ -64,12 +66,7 @@ macro_rules! map_insert_seq_bench {
macro_rules! map_find_rand_bench {
($name: ident, $n: expr, $map: ident) => (
#[bench]
pub fn $name(b: &mut ::test::Bencher) {
use std::iter::Iterator;
use std::__rand::{thread_rng, Rng};
use std::vec::Vec;
use test::black_box;

pub fn $name(b: &mut Bencher) {
let mut map = $map::new();
let n: usize = $n;

Expand Down Expand Up @@ -97,9 +94,7 @@ macro_rules! map_find_rand_bench {
macro_rules! map_find_seq_bench {
($name: ident, $n: expr, $map: ident) => (
#[bench]
pub fn $name(b: &mut ::test::Bencher) {
use test::black_box;

pub fn $name(b: &mut Bencher) {
let mut map = $map::new();
let n: usize = $n;

Expand All @@ -118,3 +113,45 @@ macro_rules! map_find_seq_bench {
}
)
}

map_insert_rand_bench!{insert_rand_100, 100, BTreeMap}
map_insert_rand_bench!{insert_rand_10_000, 10_000, BTreeMap}

map_insert_seq_bench!{insert_seq_100, 100, BTreeMap}
map_insert_seq_bench!{insert_seq_10_000, 10_000, BTreeMap}

map_find_rand_bench!{find_rand_100, 100, BTreeMap}
map_find_rand_bench!{find_rand_10_000, 10_000, BTreeMap}

map_find_seq_bench!{find_seq_100, 100, BTreeMap}
map_find_seq_bench!{find_seq_10_000, 10_000, BTreeMap}

fn bench_iter(b: &mut Bencher, size: i32) {
let mut map = BTreeMap::<i32, i32>::new();
let mut rng = thread_rng();

for _ in 0..size {
map.insert(rng.gen(), rng.gen());
}

b.iter(|| {
for entry in &map {
black_box(entry);
}
});
}

#[bench]
pub fn iter_20(b: &mut Bencher) {
bench_iter(b, 20);
}

#[bench]
pub fn iter_1000(b: &mut Bencher) {
bench_iter(b, 1000);
}

#[bench]
pub fn iter_100000(b: &mut Bencher) {
bench_iter(b, 100000);
}
11 changes: 11 additions & 0 deletions src/libcollections/benches/btree/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright 2017 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.

mod map;
24 changes: 24 additions & 0 deletions src/libcollections/benches/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2017 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.

#![deny(warnings)]

#![feature(rand)]
#![feature(test)]

extern crate test;

mod btree;
mod linked_list;
mod string;
mod str;
mod slice;
mod vec;
mod vec_deque;
87 changes: 87 additions & 0 deletions src/libcollections/benches/linked_list.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright 2017 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.

use std::collections::LinkedList;
use test::Bencher;

#[bench]
fn bench_collect_into(b: &mut Bencher) {
let v = &[0; 64];
b.iter(|| {
let _: LinkedList<_> = v.iter().cloned().collect();
})
}

#[bench]
fn bench_push_front(b: &mut Bencher) {
let mut m: LinkedList<_> = LinkedList::new();
b.iter(|| {
m.push_front(0);
})
}

#[bench]
fn bench_push_back(b: &mut Bencher) {
let mut m: LinkedList<_> = LinkedList::new();
b.iter(|| {
m.push_back(0);
})
}

#[bench]
fn bench_push_back_pop_back(b: &mut Bencher) {
let mut m: LinkedList<_> = LinkedList::new();
b.iter(|| {
m.push_back(0);
m.pop_back();
})
}

#[bench]
fn bench_push_front_pop_front(b: &mut Bencher) {
let mut m: LinkedList<_> = LinkedList::new();
b.iter(|| {
m.push_front(0);
m.pop_front();
})
}

#[bench]
fn bench_iter(b: &mut Bencher) {
let v = &[0; 128];
let m: LinkedList<_> = v.iter().cloned().collect();
b.iter(|| {
assert!(m.iter().count() == 128);
})
}
#[bench]
fn bench_iter_mut(b: &mut Bencher) {
let v = &[0; 128];
let mut m: LinkedList<_> = v.iter().cloned().collect();
b.iter(|| {
assert!(m.iter_mut().count() == 128);
})
}
#[bench]
fn bench_iter_rev(b: &mut Bencher) {
let v = &[0; 128];
let m: LinkedList<_> = v.iter().cloned().collect();
b.iter(|| {
assert!(m.iter().rev().count() == 128);
})
}
#[bench]
fn bench_iter_mut_rev(b: &mut Bencher) {
let v = &[0; 128];
let mut m: LinkedList<_> = v.iter().cloned().collect();
b.iter(|| {
assert!(m.iter_mut().rev().count() == 128);
})
}
Loading

0 comments on commit 2762b4d

Please sign in to comment.