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.
|
|
|
|
|
2012-09-11 19:46:20 -05:00
|
|
|
extern mod std;
|
2013-02-01 01:13:36 -06:00
|
|
|
use std::oldmap;
|
2013-01-23 18:51:49 -06:00
|
|
|
use std::treemap::TreeMap;
|
2013-01-23 09:25:27 -06:00
|
|
|
use core::hashmap::linear::*;
|
2012-12-28 21:57:18 -06:00
|
|
|
use core::io::WriterUtil;
|
2012-08-21 17:55:17 -05:00
|
|
|
|
|
|
|
struct Results {
|
2012-09-07 16:50:47 -05:00
|
|
|
sequential_ints: float,
|
|
|
|
random_ints: float,
|
|
|
|
delete_ints: float,
|
2012-08-21 17:55:17 -05:00
|
|
|
|
2012-09-07 16:50:47 -05:00
|
|
|
sequential_strings: float,
|
|
|
|
random_strings: float,
|
|
|
|
delete_strings: float
|
2012-08-21 17:55:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn timed(result: &mut float,
|
|
|
|
op: fn()) {
|
|
|
|
let start = std::time::precise_time_s();
|
|
|
|
op();
|
|
|
|
let end = std::time::precise_time_s();
|
|
|
|
*result = (end - start);
|
|
|
|
}
|
|
|
|
|
2013-01-23 18:51:49 -06:00
|
|
|
fn old_int_benchmarks(rng: @rand::Rng, num_keys: uint, results: &mut Results) {
|
2012-08-21 17:55:17 -05:00
|
|
|
|
|
|
|
{
|
2013-02-01 01:13:36 -06:00
|
|
|
let map = oldmap::HashMap();
|
2012-08-21 17:55:17 -05:00
|
|
|
do timed(&mut results.sequential_ints) {
|
|
|
|
for uint::range(0, num_keys) |i| {
|
|
|
|
map.insert(i, i+1);
|
|
|
|
}
|
|
|
|
|
|
|
|
for uint::range(0, num_keys) |i| {
|
2013-02-03 22:29:17 -06:00
|
|
|
assert map.get(&i) == i+1;
|
2012-08-21 17:55:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2013-02-01 01:13:36 -06:00
|
|
|
let map = oldmap::HashMap();
|
2012-08-21 17:55:17 -05:00
|
|
|
do timed(&mut results.random_ints) {
|
|
|
|
for uint::range(0, num_keys) |i| {
|
|
|
|
map.insert(rng.next() as uint, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2013-02-01 01:13:36 -06:00
|
|
|
let map = oldmap::HashMap();
|
2012-08-21 17:55:17 -05:00
|
|
|
for uint::range(0, num_keys) |i| {
|
|
|
|
map.insert(i, i);;
|
|
|
|
}
|
|
|
|
|
|
|
|
do timed(&mut results.delete_ints) {
|
|
|
|
for uint::range(0, num_keys) |i| {
|
2013-02-03 16:12:33 -06:00
|
|
|
assert map.remove(&i);
|
2012-08-21 17:55:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-23 18:51:49 -06:00
|
|
|
fn old_str_benchmarks(rng: @rand::Rng, num_keys: uint, results: &mut Results) {
|
2012-08-21 17:55:17 -05:00
|
|
|
{
|
2013-02-01 01:13:36 -06:00
|
|
|
let map = oldmap::HashMap();
|
2012-08-21 17:55:17 -05:00
|
|
|
do timed(&mut results.sequential_strings) {
|
|
|
|
for uint::range(0, num_keys) |i| {
|
2013-02-02 09:40:42 -06:00
|
|
|
let s = uint::to_str(i);
|
2012-08-21 17:55:17 -05:00
|
|
|
map.insert(s, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
for uint::range(0, num_keys) |i| {
|
2013-02-02 09:40:42 -06:00
|
|
|
let s = uint::to_str(i);
|
2013-02-03 22:29:17 -06:00
|
|
|
assert map.get(&s) == i;
|
2012-08-21 17:55:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2013-02-01 01:13:36 -06:00
|
|
|
let map = oldmap::HashMap();
|
2012-08-21 17:55:17 -05:00
|
|
|
do timed(&mut results.random_strings) {
|
|
|
|
for uint::range(0, num_keys) |i| {
|
2013-02-02 09:40:42 -06:00
|
|
|
let s = uint::to_str(rng.next() as uint);
|
2012-08-21 17:55:17 -05:00
|
|
|
map.insert(s, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2013-02-01 01:13:36 -06:00
|
|
|
let map = oldmap::HashMap();
|
2012-08-21 17:55:17 -05:00
|
|
|
for uint::range(0, num_keys) |i| {
|
2013-02-02 09:40:42 -06:00
|
|
|
map.insert(uint::to_str(i), i);
|
2012-08-21 17:55:17 -05:00
|
|
|
}
|
|
|
|
do timed(&mut results.delete_strings) {
|
|
|
|
for uint::range(0, num_keys) |i| {
|
2013-02-03 19:55:19 -06:00
|
|
|
assert map.remove(&uint::to_str(i));
|
2012-08-21 17:55:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-23 18:51:49 -06:00
|
|
|
fn linear_int_benchmarks(rng: @rand::Rng, num_keys: uint, results: &mut Results) {
|
|
|
|
{
|
|
|
|
let mut map = LinearMap::new();
|
|
|
|
do timed(&mut results.sequential_ints) {
|
|
|
|
for uint::range(0, num_keys) |i| {
|
|
|
|
map.insert(i, i+1);
|
|
|
|
}
|
|
|
|
|
|
|
|
for uint::range(0, num_keys) |i| {
|
|
|
|
assert map.find(&i).unwrap() == &(i+1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut map = LinearMap::new();
|
|
|
|
do timed(&mut results.random_ints) {
|
|
|
|
for uint::range(0, num_keys) |i| {
|
|
|
|
map.insert(rng.next() as uint, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut map = LinearMap::new();
|
|
|
|
for uint::range(0, num_keys) |i| {
|
|
|
|
map.insert(i, i);;
|
|
|
|
}
|
|
|
|
|
|
|
|
do timed(&mut results.delete_ints) {
|
|
|
|
for uint::range(0, num_keys) |i| {
|
|
|
|
assert map.remove(&i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn linear_str_benchmarks(rng: @rand::Rng, num_keys: uint, results: &mut Results) {
|
|
|
|
{
|
|
|
|
let mut map = LinearMap::new();
|
|
|
|
do timed(&mut results.sequential_strings) {
|
|
|
|
for uint::range(0, num_keys) |i| {
|
2013-02-02 09:40:42 -06:00
|
|
|
let s = uint::to_str(i);
|
2013-01-23 18:51:49 -06:00
|
|
|
map.insert(s, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
for uint::range(0, num_keys) |i| {
|
2013-02-02 09:40:42 -06:00
|
|
|
let s = uint::to_str(i);
|
2013-01-23 18:51:49 -06:00
|
|
|
assert map.find(&s).unwrap() == &i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut map = LinearMap::new();
|
|
|
|
do timed(&mut results.random_strings) {
|
|
|
|
for uint::range(0, num_keys) |i| {
|
2013-02-02 09:40:42 -06:00
|
|
|
let s = uint::to_str(rng.next() as uint);
|
2013-01-23 18:51:49 -06:00
|
|
|
map.insert(s, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut map = LinearMap::new();
|
|
|
|
for uint::range(0, num_keys) |i| {
|
2013-02-02 09:40:42 -06:00
|
|
|
map.insert(uint::to_str(i), i);
|
2013-01-23 18:51:49 -06:00
|
|
|
}
|
|
|
|
do timed(&mut results.delete_strings) {
|
|
|
|
for uint::range(0, num_keys) |i| {
|
2013-02-02 09:40:42 -06:00
|
|
|
assert map.remove(&uint::to_str(i));
|
2013-01-23 18:51:49 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn tree_int_benchmarks(rng: @rand::Rng, num_keys: uint, results: &mut Results) {
|
|
|
|
{
|
|
|
|
let mut map = TreeMap::new();
|
|
|
|
do timed(&mut results.sequential_ints) {
|
|
|
|
for uint::range(0, num_keys) |i| {
|
|
|
|
map.insert(i, i+1);
|
|
|
|
}
|
|
|
|
|
|
|
|
for uint::range(0, num_keys) |i| {
|
|
|
|
assert map.find(&i).unwrap() == &(i+1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut map = TreeMap::new();
|
|
|
|
do timed(&mut results.random_ints) {
|
|
|
|
for uint::range(0, num_keys) |i| {
|
|
|
|
map.insert(rng.next() as uint, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut map = TreeMap::new();
|
|
|
|
for uint::range(0, num_keys) |i| {
|
|
|
|
map.insert(i, i);;
|
|
|
|
}
|
|
|
|
|
|
|
|
do timed(&mut results.delete_ints) {
|
|
|
|
for uint::range(0, num_keys) |i| {
|
|
|
|
assert map.remove(&i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn tree_str_benchmarks(rng: @rand::Rng, num_keys: uint, results: &mut Results) {
|
|
|
|
{
|
|
|
|
let mut map = TreeMap::new();
|
|
|
|
do timed(&mut results.sequential_strings) {
|
|
|
|
for uint::range(0, num_keys) |i| {
|
2013-02-02 09:40:42 -06:00
|
|
|
let s = uint::to_str(i);
|
2013-01-23 18:51:49 -06:00
|
|
|
map.insert(s, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
for uint::range(0, num_keys) |i| {
|
2013-02-02 09:40:42 -06:00
|
|
|
let s = uint::to_str(i);
|
2013-01-23 18:51:49 -06:00
|
|
|
assert map.find(&s).unwrap() == &i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut map = TreeMap::new();
|
|
|
|
do timed(&mut results.random_strings) {
|
|
|
|
for uint::range(0, num_keys) |i| {
|
2013-02-02 09:40:42 -06:00
|
|
|
let s = uint::to_str(rng.next() as uint);
|
2013-01-23 18:51:49 -06:00
|
|
|
map.insert(s, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut map = TreeMap::new();
|
|
|
|
for uint::range(0, num_keys) |i| {
|
2013-02-02 09:40:42 -06:00
|
|
|
map.insert(uint::to_str(i), i);
|
2013-01-23 18:51:49 -06:00
|
|
|
}
|
|
|
|
do timed(&mut results.delete_strings) {
|
|
|
|
for uint::range(0, num_keys) |i| {
|
2013-02-02 09:40:42 -06:00
|
|
|
assert map.remove(&uint::to_str(i));
|
2013-01-23 18:51:49 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-21 17:55:17 -05:00
|
|
|
fn write_header(header: &str) {
|
|
|
|
io::stdout().write_str(header);
|
|
|
|
io::stdout().write_str("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write_row(label: &str, value: float) {
|
|
|
|
io::stdout().write_str(fmt!("%30s %f s\n", label, value));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write_results(label: &str, results: &Results) {
|
|
|
|
write_header(label);
|
|
|
|
write_row("sequential_ints", results.sequential_ints);
|
|
|
|
write_row("random_ints", results.random_ints);
|
|
|
|
write_row("delete_ints", results.delete_ints);
|
|
|
|
write_row("sequential_strings", results.sequential_strings);
|
|
|
|
write_row("random_strings", results.random_strings);
|
|
|
|
write_row("delete_strings", results.delete_strings);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn empty_results() -> Results {
|
|
|
|
Results {
|
|
|
|
sequential_ints: 0f,
|
|
|
|
random_ints: 0f,
|
|
|
|
delete_ints: 0f,
|
|
|
|
|
|
|
|
sequential_strings: 0f,
|
|
|
|
random_strings: 0f,
|
|
|
|
delete_strings: 0f,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-03 21:16:27 -05:00
|
|
|
fn main() {
|
|
|
|
let args = os::args();
|
2012-08-21 17:55:17 -05:00
|
|
|
let num_keys = {
|
|
|
|
if args.len() == 2 {
|
|
|
|
uint::from_str(args[1]).get()
|
|
|
|
} else {
|
|
|
|
100 // woefully inadequate for any real measurement
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let seed = ~[1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
|
|
|
|
|
|
|
{
|
2012-09-27 13:14:59 -05:00
|
|
|
let rng = rand::seeded_rng(&seed);
|
2012-08-21 17:55:17 -05:00
|
|
|
let mut results = empty_results();
|
2013-01-23 18:51:49 -06:00
|
|
|
old_int_benchmarks(rng, num_keys, &mut results);
|
|
|
|
old_str_benchmarks(rng, num_keys, &mut results);
|
2013-02-01 01:13:36 -06:00
|
|
|
write_results("std::oldmap::HashMap", &results);
|
2013-01-23 18:51:49 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let rng = rand::seeded_rng(&seed);
|
|
|
|
let mut results = empty_results();
|
|
|
|
linear_int_benchmarks(rng, num_keys, &mut results);
|
|
|
|
linear_str_benchmarks(rng, num_keys, &mut results);
|
|
|
|
write_results("core::hashmap::linear::LinearMap", &results);
|
2012-08-21 17:55:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2012-09-27 13:14:59 -05:00
|
|
|
let rng = rand::seeded_rng(&seed);
|
2012-08-21 17:55:17 -05:00
|
|
|
let mut results = empty_results();
|
2013-01-23 18:51:49 -06:00
|
|
|
tree_int_benchmarks(rng, num_keys, &mut results);
|
|
|
|
tree_str_benchmarks(rng, num_keys, &mut results);
|
|
|
|
write_results("std::treemap::TreeMap", &results);
|
2012-08-21 17:55:17 -05:00
|
|
|
}
|
2012-12-28 21:57:18 -06:00
|
|
|
}
|