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
|
|
|
|
2013-05-20 19:07:24 -05:00
|
|
|
extern mod extra;
|
2012-06-26 13:13:02 -05:00
|
|
|
|
2013-05-20 19:07:24 -05:00
|
|
|
use extra::time::precise_time_s;
|
2013-05-24 21:35:29 -05:00
|
|
|
use std::int;
|
|
|
|
use std::io;
|
|
|
|
use std::os;
|
2013-05-20 19:07:24 -05:00
|
|
|
use std::rand::RngUtil;
|
2013-05-24 21:35:29 -05:00
|
|
|
use std::rand;
|
|
|
|
use std::result;
|
2013-07-11 13:45:45 -05:00
|
|
|
use std::str;
|
2013-05-24 21:35:29 -05:00
|
|
|
use std::uint;
|
2013-05-20 19:07:24 -05:00
|
|
|
use std::util;
|
2013-05-24 21:35:29 -05:00
|
|
|
use std::vec;
|
2012-06-26 13:13:02 -05:00
|
|
|
|
2012-11-07 15:41:02 -06:00
|
|
|
macro_rules! bench (
|
2012-12-14 13:52:11 -06:00
|
|
|
($id:ident) => (maybe_run_test(argv, stringify!($id).to_owned(), $id))
|
2012-11-07 15:41:02 -06:00
|
|
|
)
|
|
|
|
|
2012-10-03 21:16:27 -05:00
|
|
|
fn main() {
|
|
|
|
let argv = os::args();
|
2013-07-17 14:31:20 -05:00
|
|
|
let _tests = argv.slice(1, argv.len());
|
2012-06-26 16:04:15 -05:00
|
|
|
|
2012-08-22 19:24:52 -05:00
|
|
|
bench!(shift_push);
|
|
|
|
bench!(read_line);
|
|
|
|
bench!(vec_plus);
|
|
|
|
bench!(vec_append);
|
|
|
|
bench!(vec_push_all);
|
2013-07-11 13:45:45 -05:00
|
|
|
bench!(is_utf8_ascii);
|
|
|
|
bench!(is_utf8_multibyte);
|
2012-06-26 13:13:02 -05:00
|
|
|
}
|
|
|
|
|
2013-03-07 16:38:38 -06:00
|
|
|
fn maybe_run_test(argv: &[~str], name: ~str, test: &fn()) {
|
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 {
|
2013-07-04 21:13:26 -05:00
|
|
|
run_test = argv.iter().any(|x| x == &~"all") || 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-07-22 11:04:51 -05:00
|
|
|
printfln!("%s:\t\t%f ms", name, (stop - start) * 1000f);
|
2012-06-26 13:13:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn shift_push() {
|
|
|
|
let mut v1 = vec::from_elem(30000, 1);
|
2012-06-29 18:26:56 -05:00
|
|
|
let mut v2 = ~[];
|
2012-06-26 13:13:02 -05:00
|
|
|
|
|
|
|
while v1.len() > 0 {
|
2012-09-28 00:20:47 -05:00
|
|
|
v2.push(v1.shift());
|
2012-06-26 13:13:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_line() {
|
2012-08-24 17:28:43 -05:00
|
|
|
let path = Path(env!("CFG_SRC_DIR"))
|
|
|
|
.push_rel(&Path("src/test/bench/shootout-k-nucleotide.data"));
|
2012-06-26 13:13:02 -05:00
|
|
|
|
2012-06-30 18:19:07 -05:00
|
|
|
for int::range(0, 3) |_i| {
|
2013-07-11 14:05:17 -05:00
|
|
|
let reader = result::unwrap(io::file_reader(&path));
|
2012-06-26 13:13:02 -05:00
|
|
|
while !reader.eof() {
|
|
|
|
reader.read_line();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-06-26 16:04:15 -05:00
|
|
|
|
2012-06-27 18:08:22 -05:00
|
|
|
fn vec_plus() {
|
2013-05-06 21:29:04 -05:00
|
|
|
let mut r = rand::rng();
|
2012-06-27 13:32:22 -05:00
|
|
|
|
2013-02-01 01:13:36 -06:00
|
|
|
let mut v = ~[];
|
2012-06-27 18:08:22 -05:00
|
|
|
let mut i = 0;
|
|
|
|
while i < 1500 {
|
2012-06-27 13:32:22 -05:00
|
|
|
let rv = vec::from_elem(r.gen_uint_range(0, 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 {
|
2012-06-27 13:32:22 -05:00
|
|
|
v = rv + v;
|
|
|
|
}
|
2012-06-27 18:08:22 -05:00
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn vec_append() {
|
2013-05-06 21:29:04 -05:00
|
|
|
let mut r = rand::rng();
|
2012-06-27 18:08:22 -05:00
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
let mut v = ~[];
|
2012-06-27 18:08:22 -05:00
|
|
|
let mut i = 0;
|
|
|
|
while i < 1500 {
|
|
|
|
let rv = vec::from_elem(r.gen_uint_range(0, i + 1), i);
|
2013-04-24 07:29:19 -05:00
|
|
|
if r.gen() {
|
2012-06-27 18:08:22 -05:00
|
|
|
v = vec::append(v, rv);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
v = vec::append(rv, v);
|
|
|
|
}
|
|
|
|
i += 1;
|
2012-06-27 13:32:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn vec_push_all() {
|
2013-05-06 21:29:04 -05:00
|
|
|
let mut r = rand::rng();
|
2012-06-27 13:32:22 -05:00
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
let mut v = ~[];
|
2012-06-30 18:19:07 -05:00
|
|
|
for uint::range(0, 1500) |i| {
|
2012-06-27 13:32:22 -05:00
|
|
|
let mut rv = vec::from_elem(r.gen_uint_range(0, i + 1), i);
|
2013-04-24 07:29:19 -05:00
|
|
|
if r.gen() {
|
2012-09-26 19:33:34 -05:00
|
|
|
v.push_all(rv);
|
2012-06-27 13:32:22 -05:00
|
|
|
}
|
|
|
|
else {
|
2013-05-05 23:42:54 -05:00
|
|
|
util::swap(&mut v, &mut rv);
|
2012-09-26 19:33:34 -05:00
|
|
|
v.push_all(rv);
|
2012-06-27 13:32:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-11 13:45:45 -05:00
|
|
|
|
|
|
|
fn is_utf8_ascii() {
|
|
|
|
let mut v : ~[u8] = ~[];
|
|
|
|
for uint::range(0, 20000) |_| {
|
|
|
|
v.push('b' as u8);
|
|
|
|
if !str::is_utf8(v) {
|
|
|
|
fail!("is_utf8 failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_utf8_multibyte() {
|
|
|
|
let s = "b¢€𤭢";
|
|
|
|
let mut v : ~[u8]= ~[];
|
|
|
|
for uint::range(0, 5000) |_| {
|
|
|
|
v.push_all(s.as_bytes());
|
|
|
|
if !str::is_utf8(v) {
|
|
|
|
fail!("is_utf8 failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|