perf: improved the core-std benchmark

This commit is contained in:
Eric Holk 2012-06-26 14:04:15 -07:00
parent a08281616f
commit ccd2394ff9
2 changed files with 29 additions and 3 deletions

View File

@ -326,7 +326,7 @@ Convert a set into a vector.
fn vec_from_set<T: copy>(s: set<T>) -> [T]/~ {
let mut v = []/~;
s.each_key() {|k|
v += [k]/~;
vec::push(v, k);
true
};
v

View File

@ -3,17 +3,25 @@
use std;
import std::time::precise_time_s;
import std::map;
import std::map::{map, hashmap};
import io::{reader, reader_util};
fn main() {
fn main(argv: [str]/~) {
#macro[
[#bench[id],
run_test(#stringify(id), id)]
if tests.len() == 0 || vec::contains(tests, #stringify(id)) {
run_test(#stringify(id), id);
}
]
];
let tests = vec::view(argv, 1, argv.len());
#bench[shift_push];
#bench[read_line];
#bench[str_set];
}
fn run_test(name: str, test: fn()) {
@ -46,3 +54,21 @@ fn read_line() {
}
}
}
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 { }
}
}
}