rust/src/test/bench/core-std.rs

128 lines
2.5 KiB
Rust
Raw Normal View History

2012-06-26 13:13:02 -05:00
// Microbenchmarks for various functions in core and std
use std;
import std::time::precise_time_s;
2012-06-26 16:04:15 -05:00
import std::map;
import std::map::{map, hashmap};
2012-06-26 13:13:02 -05:00
import io::{reader, reader_util};
2012-06-26 16:04:15 -05:00
fn main(argv: [str]/~) {
2012-06-26 13:13:02 -05:00
#macro[
[#bench[id],
2012-06-26 16:04:15 -05:00
if tests.len() == 0 || vec::contains(tests, #stringify(id)) {
run_test(#stringify(id), id);
}
]
2012-06-26 13:13:02 -05:00
];
2012-06-26 16:04:15 -05:00
let tests = vec::view(argv, 1, argv.len());
2012-06-26 13:13:02 -05:00
#bench[shift_push];
#bench[read_line];
2012-06-26 16:04:15 -05:00
#bench[str_set];
#bench[vec_plus];
#bench[vec_append];
#bench[vec_push_all];
2012-06-26 13:13:02 -05:00
}
fn run_test(name: str, test: fn()) {
let start = precise_time_s();
test();
let stop = precise_time_s();
io::println(#fmt("%s:\t\t%f ms", name, (stop - start) * 1000f));
}
fn shift_push() {
let mut v1 = vec::from_elem(30000, 1);
let mut v2 = []/~;
while v1.len() > 0 {
vec::push(v2, vec::shift(v1));
}
}
fn read_line() {
let path = path::connect(
#env("CFG_SRC_DIR"),
"src/test/bench/shootout-k-nucleotide.data"
);
for int::range(0, 3) {|_i|
let reader = result::get(io::file_reader(path));
while !reader.eof() {
reader.read_line();
}
}
}
2012-06-26 16:04:15 -05:00
fn str_set() {
let r = rand::rng();
let s = map::hashmap(str::hash, str::eq);
for int::range(0, 1000) {|_i|
map::set_add(s, r.gen_str(10));
}
let mut found = 0;
for int::range(0, 1000) {|_i|
alt s.find(r.gen_str(10)) {
some(_) { found += 1; }
none { }
}
}
}
fn vec_plus() {
let r = rand::rng();
let mut v = []/~;
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 += rv;
}
else {
v = rv + v;
}
i += 1;
}
}
fn vec_append() {
let r = rand::rng();
let mut v = []/~;
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;
}
}
fn vec_push_all() {
let r = rand::rng();
let mut v = []/~;
for uint::range(0, 1500) {|i|
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);
}
}
}