2012-12-10 19:32:48 -06:00
|
|
|
// Copyright 2012 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.
|
|
|
|
|
2013-05-20 19:07:24 -05:00
|
|
|
// Microbenchmarks for various functions in std and extra
|
2012-06-26 13:13:02 -05:00
|
|
|
|
2014-04-14 10:30:31 -05:00
|
|
|
#![feature(macro_rules)]
|
2013-10-02 22:00:54 -05:00
|
|
|
|
2014-02-19 21:08:12 -06:00
|
|
|
extern crate time;
|
2012-06-26 13:13:02 -05:00
|
|
|
|
2014-02-19 21:08:12 -06:00
|
|
|
use time::precise_time_s;
|
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-01-31 14:35:36 -06:00
|
|
|
use std::mem::swap;
|
2013-05-24 21:35:29 -05:00
|
|
|
use std::os;
|
2013-07-11 13:45:45 -05:00
|
|
|
use std::str;
|
2014-03-05 16:02:44 -06:00
|
|
|
use std::vec;
|
2013-11-11 00:46:32 -06:00
|
|
|
use std::io::File;
|
2012-06-26 13:13:02 -05:00
|
|
|
|
2012-11-07 15:41:02 -06:00
|
|
|
macro_rules! bench (
|
2014-05-12 19:56:43 -05:00
|
|
|
($argv:expr, $id:ident) => (maybe_run_test($argv.as_slice(),
|
2014-05-25 05:17:19 -05:00
|
|
|
stringify!($id).to_string(),
|
2014-05-12 19:56:43 -05:00
|
|
|
$id))
|
2012-11-07 15:41:02 -06:00
|
|
|
)
|
|
|
|
|
2012-10-03 21:16:27 -05:00
|
|
|
fn main() {
|
2014-05-25 05:17:19 -05:00
|
|
|
let argv = os::args().move_iter().map(|x| x.to_string()).collect::<Vec<String>>();
|
2013-07-17 14:31:20 -05:00
|
|
|
let _tests = argv.slice(1, argv.len());
|
2012-06-26 16:04:15 -05:00
|
|
|
|
2013-07-26 15:10:56 -05:00
|
|
|
bench!(argv, shift_push);
|
|
|
|
bench!(argv, read_line);
|
|
|
|
bench!(argv, vec_plus);
|
|
|
|
bench!(argv, vec_append);
|
|
|
|
bench!(argv, vec_push_all);
|
|
|
|
bench!(argv, is_utf8_ascii);
|
|
|
|
bench!(argv, is_utf8_multibyte);
|
2012-06-26 13:13:02 -05:00
|
|
|
}
|
|
|
|
|
2014-05-22 18:57:53 -05:00
|
|
|
fn maybe_run_test(argv: &[String], name: String, test: ||) {
|
2012-06-28 00:30:08 -05:00
|
|
|
let mut run_test = false;
|
|
|
|
|
2013-07-17 14:31:20 -05:00
|
|
|
if os::getenv("RUST_BENCH").is_some() {
|
2013-05-06 21:29:04 -05:00
|
|
|
run_test = true
|
|
|
|
} else if argv.len() > 0 {
|
2014-05-25 05:17:19 -05:00
|
|
|
run_test = argv.iter().any(|x| x == &"all".to_string()) || argv.iter().any(|x| x == &name)
|
2012-06-28 00:30:08 -05:00
|
|
|
}
|
|
|
|
|
2013-05-06 21:29:04 -05:00
|
|
|
if !run_test {
|
|
|
|
return
|
|
|
|
}
|
2012-06-28 00:30:08 -05:00
|
|
|
|
2012-06-26 13:13:02 -05:00
|
|
|
let start = precise_time_s();
|
|
|
|
test();
|
|
|
|
let stop = precise_time_s();
|
|
|
|
|
2013-09-26 01:26:09 -05:00
|
|
|
println!("{}:\t\t{} ms", name, (stop - start) * 1000.0);
|
2012-06-26 13:13:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn shift_push() {
|
2014-04-21 16:58:52 -05:00
|
|
|
let mut v1 = Vec::from_elem(30000, 1i);
|
2014-03-05 16:02:44 -06:00
|
|
|
let mut v2 = Vec::new();
|
2012-06-26 13:13:02 -05:00
|
|
|
|
|
|
|
while v1.len() > 0 {
|
2013-12-23 09:40:42 -06:00
|
|
|
v2.push(v1.shift().unwrap());
|
2012-06-26 13:13:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_line() {
|
2014-01-15 15:25:09 -06:00
|
|
|
use std::io::BufferedReader;
|
2013-10-06 18:08:56 -05:00
|
|
|
|
2013-12-03 21:15:12 -06:00
|
|
|
let mut path = Path::new(env!("CFG_SRC_DIR"));
|
2013-10-05 21:49:32 -05:00
|
|
|
path.push("src/test/bench/shootout-k-nucleotide.data");
|
2012-06-26 13:13:02 -05:00
|
|
|
|
2014-04-21 16:58:52 -05:00
|
|
|
for _ in range(0u, 3) {
|
2013-10-30 01:31:07 -05:00
|
|
|
let mut reader = BufferedReader::new(File::open(&path).unwrap());
|
2013-12-08 01:12:07 -06:00
|
|
|
for _line in reader.lines() {
|
2012-06-26 13:13:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-06-26 16:04:15 -05:00
|
|
|
|
2012-06-27 18:08:22 -05:00
|
|
|
fn vec_plus() {
|
2014-03-01 19:59:35 -06:00
|
|
|
let mut r = rand::task_rng();
|
2012-06-27 13:32:22 -05:00
|
|
|
|
2014-03-05 16:02:44 -06:00
|
|
|
let mut v = Vec::new();
|
2012-06-27 18:08:22 -05:00
|
|
|
let mut i = 0;
|
|
|
|
while i < 1500 {
|
2014-03-05 17:28:08 -06:00
|
|
|
let rv = Vec::from_elem(r.gen_range(0u, i + 1), i);
|
2013-04-24 07:29:19 -05:00
|
|
|
if r.gen() {
|
2013-06-11 21:13:42 -05:00
|
|
|
v.push_all_move(rv);
|
|
|
|
} else {
|
2014-03-30 22:53:26 -05:00
|
|
|
v = rv.clone().append(v.as_slice());
|
2012-06-27 13:32:22 -05:00
|
|
|
}
|
2012-06-27 18:08:22 -05:00
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn vec_append() {
|
2014-03-01 19:59:35 -06:00
|
|
|
let mut r = rand::task_rng();
|
2012-06-27 18:08:22 -05:00
|
|
|
|
2014-03-05 16:02:44 -06:00
|
|
|
let mut v = Vec::new();
|
2012-06-27 18:08:22 -05:00
|
|
|
let mut i = 0;
|
|
|
|
while i < 1500 {
|
2014-03-05 17:28:08 -06:00
|
|
|
let rv = Vec::from_elem(r.gen_range(0u, i + 1), i);
|
2013-04-24 07:29:19 -05:00
|
|
|
if r.gen() {
|
2014-03-30 22:53:26 -05:00
|
|
|
v = v.clone().append(rv.as_slice());
|
2012-06-27 18:08:22 -05:00
|
|
|
}
|
|
|
|
else {
|
2014-03-30 22:53:26 -05:00
|
|
|
v = rv.clone().append(v.as_slice());
|
2012-06-27 18:08:22 -05:00
|
|
|
}
|
|
|
|
i += 1;
|
2012-06-27 13:32:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn vec_push_all() {
|
2014-03-01 19:59:35 -06:00
|
|
|
let mut r = rand::task_rng();
|
2012-06-27 13:32:22 -05:00
|
|
|
|
2014-03-05 16:02:44 -06:00
|
|
|
let mut v = Vec::new();
|
2013-08-03 11:45:23 -05:00
|
|
|
for i in range(0u, 1500) {
|
2014-03-05 17:28:08 -06:00
|
|
|
let mut rv = Vec::from_elem(r.gen_range(0u, i + 1), i);
|
2013-04-24 07:29:19 -05:00
|
|
|
if r.gen() {
|
2014-03-05 17:28:08 -06:00
|
|
|
v.push_all(rv.as_slice());
|
2012-06-27 13:32:22 -05:00
|
|
|
}
|
|
|
|
else {
|
2014-01-31 14:35:36 -06:00
|
|
|
swap(&mut v, &mut rv);
|
2014-03-05 17:28:08 -06:00
|
|
|
v.push_all(rv.as_slice());
|
2012-06-27 13:32:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-11 13:45:45 -05:00
|
|
|
|
|
|
|
fn is_utf8_ascii() {
|
2014-03-05 16:02:44 -06:00
|
|
|
let mut v : Vec<u8> = Vec::new();
|
2013-08-03 11:45:23 -05:00
|
|
|
for _ in range(0u, 20000) {
|
2013-07-11 13:45:45 -05:00
|
|
|
v.push('b' as u8);
|
2014-03-05 17:28:08 -06:00
|
|
|
if !str::is_utf8(v.as_slice()) {
|
2013-10-21 15:08:31 -05:00
|
|
|
fail!("is_utf8 failed");
|
2013-07-11 13:45:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_utf8_multibyte() {
|
|
|
|
let s = "b¢€𤭢";
|
2014-03-05 16:02:44 -06:00
|
|
|
let mut v : Vec<u8> = Vec::new();
|
2013-08-03 11:45:23 -05:00
|
|
|
for _ in range(0u, 5000) {
|
2013-07-11 13:45:45 -05:00
|
|
|
v.push_all(s.as_bytes());
|
2014-03-05 17:28:08 -06:00
|
|
|
if !str::is_utf8(v.as_slice()) {
|
2013-10-21 15:08:31 -05:00
|
|
|
fail!("is_utf8 failed");
|
2013-07-11 13:45:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|