From 97522ab7660372dcac5d2ac6bf1df5f9ae28b8ce Mon Sep 17 00:00:00 2001 From: Chris Morgan Date: Thu, 12 Jun 2014 22:25:17 +1000 Subject: [PATCH] Add some simple benchmarks. --- Makefile | 3 +++ src/lib.rs | 28 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/Makefile b/Makefile index 863c961..3414035 100644 --- a/Makefile +++ b/Makefile @@ -39,6 +39,9 @@ build/quicktest: $(SRC) # build/test --test test: anymap doctest +bench: anymap build/test + build/test --bench + doctest: $(SRC) $(LIB) $(RUSTDOC) -L build --test src/lib.rs diff --git a/src/lib.rs b/src/lib.rs index 8bc9a17..cdcc6ff 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,6 +7,9 @@ variant_size_difference, managed_heap_memory, unnecessary_typecast, missing_doc, unused_result, deprecated_owned_vector)] +#[cfg(test)] +extern crate test; + use std::any::{Any, AnyRefExt, AnyMutRefExt}; use std::intrinsics::TypeId; use std::collections::HashMap; @@ -71,3 +74,28 @@ impl AnyMap { self.data.remove(&TypeId::of::()); } } + +#[bench] +fn bench_insertion(b: &mut ::test::Bencher) { + b.iter(|| { + let mut data = AnyMap::new(); + data.insert(42i); + }) +} + +#[bench] +fn bench_find_missing(b: &mut ::test::Bencher) { + b.iter(|| { + let data = AnyMap::new(); + assert_eq!(data.find(), None::<&int>); + }) +} + +#[bench] +fn bench_find_present(b: &mut ::test::Bencher) { + b.iter(|| { + let mut data = AnyMap::new(); + data.insert(42i); + assert_eq!(data.find(), Some(&42i)); + }) +}