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