Add some simple benchmarks.
authorChris Morgan <me@chrismorgan.info>
committerChris Morgan <me@chrismorgan.info>
Makefile
src/lib.rs

index 863c9612870f96a6edcf114ccf4c192e7e5c81b3..3414035a6d36d9ba1cbfdb94af17e4181b79f1a8 100644 (file)
--- 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
 
index 8bc9a178a054ed6fc43c36d5f9b83d38bd997310..cdcc6fffb847134f0d4e0d506abe142655b51612 100644 (file)
@@ -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::<T>());
     }
 }
+
+#[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));
+    })
+}