Skip to content

Instantly share code, notes, and snippets.

@japaric
Created November 22, 2014 18:30
Show Gist options
  • Save japaric/c16a573b8e371697c9f1 to your computer and use it in GitHub Desktop.
Save japaric/c16a573b8e371697c9f1 to your computer and use it in GitHub Desktop.
[package]
name = "map"
version = "0.0.0"
authors = ["Jorge Aparicio <[email protected]>"]
[dependencies.criterion]
git = "https://github.com/japaric/criterion.rs"
extern crate criterion;
use criterion::Criterion;
use std::collections::{HashMap, TreeMap};
use std::time::duration::Duration;
const SIZES: &'static [uint] = &[100, 1_000, 10_000, 100_000, 1_000_000];
fn main() {
Criterion::default().
//measurement_time(Duration::seconds(60)).
bench("ayosec", |b| {
let mut map = HashMap::new();
let mut key = 0u;
b.iter(|| {
key = key + 1;
map.insert(key, 0u);
if key > 100 && key % 10 == 0 {
let r = key - 17;
map.remove(&r);
map.insert(key + 13, 1);
map.insert(key + 11, 1);
}
})
}).
bench_with_inputs("hashmap", |b, &n| {
// Start with a map filled with N elements
let mut m: HashMap<_, _> = range(0, n).map(|i| (i, 0u)).collect();
let idx = n + 1;
b.iter(|| {
m.insert(idx, 0u);
m.remove(&idx);
})
}, SIZES).
bench_with_inputs("treemap", |b, &n| {
// Start with a map filled with N elements
let mut m: TreeMap<_, _> = range(0, n).map(|i| (i, 0u)).collect();
let idx = n + 1;
b.iter(|| {
m.insert(idx, 0u);
m.remove(&idx);
})
}, SIZES);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment