2012-06-26 13:13:02 -05:00
|
|
|
// Microbenchmarks for various functions in core and std
|
|
|
|
|
2012-09-11 19:46:20 -05:00
|
|
|
extern mod std;
|
2012-06-26 13:13:02 -05:00
|
|
|
|
2012-09-05 14:32:05 -05:00
|
|
|
use std::time::precise_time_s;
|
|
|
|
use std::map;
|
2012-09-10 17:38:28 -05:00
|
|
|
use std::map::{Map, HashMap};
|
2012-06-26 13:13:02 -05:00
|
|
|
|
2012-09-05 14:32:05 -05:00
|
|
|
use io::{Reader, ReaderUtil};
|
2012-06-26 13:13:02 -05:00
|
|
|
|
2012-09-20 17:59:09 -05:00
|
|
|
fn main(++argv: ~[~str]) {
|
2012-06-26 13:13:02 -05:00
|
|
|
#macro[
|
|
|
|
[#bench[id],
|
2012-06-28 00:30:08 -05:00
|
|
|
maybe_run_test(argv, #stringify(id), id)
|
2012-06-26 16:04:15 -05:00
|
|
|
]
|
2012-06-26 13:13:02 -05:00
|
|
|
];
|
|
|
|
|
2012-07-26 15:43:27 -05:00
|
|
|
let tests = vec::view(argv, 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!(str_set);
|
|
|
|
bench!(vec_plus);
|
|
|
|
bench!(vec_append);
|
|
|
|
bench!(vec_push_all);
|
2012-06-26 13:13:02 -05:00
|
|
|
}
|
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
fn maybe_run_test(argv: &[~str], name: ~str, test: fn()) {
|
2012-06-28 00:30:08 -05:00
|
|
|
let mut run_test = false;
|
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
if os::getenv(~"RUST_BENCH").is_some() { run_test = true }
|
2012-06-28 00:30:08 -05:00
|
|
|
else if argv.len() > 0 {
|
2012-07-14 00:57:48 -05:00
|
|
|
run_test = argv.contains(~"all") || argv.contains(name)
|
2012-06-28 00:30:08 -05:00
|
|
|
}
|
|
|
|
|
2012-08-01 19:30:05 -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();
|
|
|
|
|
2012-08-22 19:24:52 -05:00
|
|
|
io::println(fmt!("%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 {
|
|
|
|
vec::push(v2, vec::shift(v1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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| {
|
2012-08-24 17:28:43 -05:00
|
|
|
let reader = result::get(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
|
|
|
|
|
|
|
fn str_set() {
|
2012-08-27 16:22:25 -05:00
|
|
|
let r = rand::Rng();
|
2012-06-26 16:04:15 -05:00
|
|
|
|
2012-09-10 17:38:28 -05:00
|
|
|
let s = map::HashMap();
|
2012-06-26 16:04:15 -05:00
|
|
|
|
2012-06-30 18:19:07 -05:00
|
|
|
for int::range(0, 1000) |_i| {
|
2012-06-26 16:04:15 -05:00
|
|
|
map::set_add(s, r.gen_str(10));
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut found = 0;
|
2012-06-30 18:19:07 -05:00
|
|
|
for int::range(0, 1000) |_i| {
|
2012-08-06 14:34:08 -05:00
|
|
|
match s.find(r.gen_str(10)) {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(_) => { found += 1; }
|
|
|
|
None => { }
|
2012-06-26 16:04:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-06-27 13:32:22 -05:00
|
|
|
|
2012-06-27 18:08:22 -05:00
|
|
|
fn vec_plus() {
|
2012-08-27 16:22:25 -05:00
|
|
|
let r = rand::Rng();
|
2012-06-27 13:32: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 {
|
2012-06-27 13:32:22 -05:00
|
|
|
let rv = vec::from_elem(r.gen_uint_range(0, i + 1), i);
|
|
|
|
if r.gen_bool() {
|
|
|
|
v += rv;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
v = rv + v;
|
|
|
|
}
|
2012-06-27 18:08:22 -05:00
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn vec_append() {
|
2012-08-27 16:22:25 -05:00
|
|
|
let 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);
|
|
|
|
if r.gen_bool() {
|
|
|
|
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() {
|
2012-08-27 16:22:25 -05:00
|
|
|
let 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);
|
|
|
|
if r.gen_bool() {
|
|
|
|
vec::push_all(v, rv);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
v <-> rv;
|
|
|
|
vec::push_all(v, rv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|