2013-07-10 08:27:14 -05:00
|
|
|
// Copyright 2013 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.
|
|
|
|
|
2014-03-30 07:35:54 -05:00
|
|
|
//! Container traits for collections
|
2013-07-10 08:27:14 -05:00
|
|
|
|
2013-07-19 16:07:00 -05:00
|
|
|
#[cfg(test)]
|
2013-10-05 16:44:37 -05:00
|
|
|
pub mod bench {
|
2014-05-29 21:03:06 -05:00
|
|
|
use std::prelude::*;
|
std: Recreate a `rand` module
This commit shuffles around some of the `rand` code, along with some
reorganization. The new state of the world is as follows:
* The librand crate now only depends on libcore. This interface is experimental.
* The standard library has a new module, `std::rand`. This interface will
eventually become stable.
Unfortunately, this entailed more of a breaking change than just shuffling some
names around. The following breaking changes were made to the rand library:
* Rng::gen_vec() was removed. This has been replaced with Rng::gen_iter() which
will return an infinite stream of random values. Previous behavior can be
regained with `rng.gen_iter().take(n).collect()`
* Rng::gen_ascii_str() was removed. This has been replaced with
Rng::gen_ascii_chars() which will return an infinite stream of random ascii
characters. Similarly to gen_iter(), previous behavior can be emulated with
`rng.gen_ascii_chars().take(n).collect()`
* {IsaacRng, Isaac64Rng, XorShiftRng}::new() have all been removed. These all
relied on being able to use an OSRng for seeding, but this is no longer
available in librand (where these types are defined). To retain the same
functionality, these types now implement the `Rand` trait so they can be
generated with a random seed from another random number generator. This allows
the stdlib to use an OSRng to create seeded instances of these RNGs.
* Rand implementations for `Box<T>` and `@T` were removed. These seemed to be
pretty rare in the codebase, and it allows for librand to not depend on
liballoc. Additionally, other pointer types like Rc<T> and Arc<T> were not
supported. If this is undesirable, librand can depend on liballoc and regain
these implementations.
* The WeightedChoice structure is no longer built with a `Vec<Weighted<T>>`,
but rather a `&mut [Weighted<T>]`. This means that the WeightedChoice
structure now has a lifetime associated with it.
* The `sample` method on `Rng` has been moved to a top-level function in the
`rand` module due to its dependence on `Vec`.
cc #13851
[breaking-change]
2014-05-25 03:39:37 -05:00
|
|
|
use std::rand;
|
|
|
|
use std::rand::Rng;
|
2014-05-29 21:03:06 -05:00
|
|
|
use test::Bencher;
|
2014-06-06 18:33:44 -05:00
|
|
|
use MutableMap;
|
2013-07-19 16:07:00 -05:00
|
|
|
|
|
|
|
pub fn insert_rand_n<M:MutableMap<uint,uint>>(n: uint,
|
|
|
|
map: &mut M,
|
2014-03-31 20:16:35 -05:00
|
|
|
b: &mut Bencher) {
|
2013-07-19 16:07:00 -05:00
|
|
|
// setup
|
2014-03-25 00:13:11 -05:00
|
|
|
let mut rng = rand::weak_rng();
|
2013-07-19 16:07:00 -05:00
|
|
|
|
|
|
|
map.clear();
|
2013-08-03 11:45:23 -05:00
|
|
|
for _ in range(0, n) {
|
2013-07-19 16:07:00 -05:00
|
|
|
map.insert(rng.gen::<uint>() % n, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// measure
|
2014-03-31 20:16:35 -05:00
|
|
|
b.iter(|| {
|
2013-07-19 16:07:00 -05:00
|
|
|
let k = rng.gen::<uint>() % n;
|
|
|
|
map.insert(k, 1);
|
|
|
|
map.remove(&k);
|
2013-11-21 21:20:48 -06:00
|
|
|
})
|
2013-07-19 16:07:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn insert_seq_n<M:MutableMap<uint,uint>>(n: uint,
|
|
|
|
map: &mut M,
|
2014-03-31 20:16:35 -05:00
|
|
|
b: &mut Bencher) {
|
2013-07-19 16:07:00 -05:00
|
|
|
// setup
|
|
|
|
map.clear();
|
2013-08-03 11:45:23 -05:00
|
|
|
for i in range(0u, n) {
|
2013-07-19 16:07:00 -05:00
|
|
|
map.insert(i*2, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// measure
|
|
|
|
let mut i = 1;
|
2014-03-31 20:16:35 -05:00
|
|
|
b.iter(|| {
|
2013-07-19 16:07:00 -05:00
|
|
|
map.insert(i, 1);
|
|
|
|
map.remove(&i);
|
|
|
|
i = (i + 2) % n;
|
2013-11-21 21:20:48 -06:00
|
|
|
})
|
2013-07-19 16:07:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn find_rand_n<M:MutableMap<uint,uint>>(n: uint,
|
|
|
|
map: &mut M,
|
2014-03-31 20:16:35 -05:00
|
|
|
b: &mut Bencher) {
|
2013-07-19 16:07:00 -05:00
|
|
|
// setup
|
2014-03-25 00:13:11 -05:00
|
|
|
let mut rng = rand::weak_rng();
|
2014-04-05 00:45:42 -05:00
|
|
|
let mut keys = Vec::from_fn(n, |_| rng.gen::<uint>() % n);
|
2013-07-19 16:07:00 -05:00
|
|
|
|
2013-08-03 11:45:23 -05:00
|
|
|
for k in keys.iter() {
|
2013-07-19 16:07:00 -05:00
|
|
|
map.insert(*k, 1);
|
|
|
|
}
|
|
|
|
|
2014-04-05 00:45:42 -05:00
|
|
|
rng.shuffle(keys.as_mut_slice());
|
2013-07-19 16:07:00 -05:00
|
|
|
|
|
|
|
// measure
|
|
|
|
let mut i = 0;
|
2014-03-31 20:16:35 -05:00
|
|
|
b.iter(|| {
|
2014-07-21 09:04:24 -05:00
|
|
|
map.find(&keys[i]);
|
2013-07-19 16:07:00 -05:00
|
|
|
i = (i + 1) % n;
|
2013-11-21 21:20:48 -06:00
|
|
|
})
|
2013-07-19 16:07:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn find_seq_n<M:MutableMap<uint,uint>>(n: uint,
|
|
|
|
map: &mut M,
|
2014-03-31 20:16:35 -05:00
|
|
|
b: &mut Bencher) {
|
2013-07-19 16:07:00 -05:00
|
|
|
// setup
|
2013-08-03 11:45:23 -05:00
|
|
|
for i in range(0u, n) {
|
2013-07-19 16:07:00 -05:00
|
|
|
map.insert(i, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// measure
|
|
|
|
let mut i = 0;
|
2014-03-31 20:16:35 -05:00
|
|
|
b.iter(|| {
|
2014-02-12 09:39:21 -06:00
|
|
|
let x = map.find(&i);
|
2013-07-19 16:07:00 -05:00
|
|
|
i = (i + 1) % n;
|
2014-02-12 09:39:21 -06:00
|
|
|
x
|
2013-11-21 21:20:48 -06:00
|
|
|
})
|
2013-07-19 16:07:00 -05:00
|
|
|
}
|
2013-08-01 17:35:46 -05:00
|
|
|
}
|
2014-06-06 18:33:44 -05:00
|
|
|
|