From ccd2394ff9ccff11950d0830aaeb74f4fabb7d9c Mon Sep 17 00:00:00 2001 From: Eric Holk Date: Tue, 26 Jun 2012 14:04:15 -0700 Subject: [PATCH] perf: improved the core-std benchmark --- src/libstd/map.rs | 2 +- src/test/bench/core-std.rs | 30 ++++++++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/libstd/map.rs b/src/libstd/map.rs index 7d1e7b599a0..00d4a5e6b31 100644 --- a/src/libstd/map.rs +++ b/src/libstd/map.rs @@ -326,7 +326,7 @@ Convert a set into a vector. fn vec_from_set(s: set) -> [T]/~ { let mut v = []/~; s.each_key() {|k| - v += [k]/~; + vec::push(v, k); true }; v diff --git a/src/test/bench/core-std.rs b/src/test/bench/core-std.rs index 823900ac55b..9ea0ad51feb 100644 --- a/src/test/bench/core-std.rs +++ b/src/test/bench/core-std.rs @@ -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 { } + } + } +}