about summary refs log tree commit diff
diff options
context:
space:
mode:
authorGraydon Hoare <graydon@mozilla.com>2013-07-22 16:50:36 -0700
committerGraydon Hoare <graydon@mozilla.com>2013-07-22 16:56:11 -0700
commitd9c0634536c8ac6cd6309221b1bbd93517686aba (patch)
tree2f74200543bb9347e1d7479bb7f4cf216e80421e
parentca5ed4cc498ad5da22c6e1c4c0b79df07a07029e (diff)
downloadrust-d9c0634536c8ac6cd6309221b1bbd93517686aba.tar.gz
rust-d9c0634536c8ac6cd6309221b1bbd93517686aba.zip
std: various additional language benchmarks in util.
-rw-r--r--src/libstd/util.rs65
1 files changed, 65 insertions, 0 deletions
diff --git a/src/libstd/util.rs b/src/libstd/util.rs
index 5ae45b74dd8..8fcfa083cb6 100644
--- a/src/libstd/util.rs
+++ b/src/libstd/util.rs
@@ -195,3 +195,68 @@ mod tests {
         unsafe { assert_eq!(did_run, true); }
     }
 }
+
+/// Completely miscellaneous language-construct benchmarks.
+#[cfg(test)]
+mod bench {
+
+    use extra::test::BenchHarness;
+    use option::{Some,None};
+
+    // Static/dynamic method dispatch
+
+    struct Struct {
+        field: int
+    }
+
+    trait Trait {
+        fn method(&self) -> int;
+    }
+
+    impl Trait for Struct {
+        fn method(&self) -> int {
+            self.field
+        }
+    }
+
+    #[bench]
+    fn trait_vtable_method_call(bh: &mut BenchHarness) {
+        let s = Struct { field: 10 };
+        let t = &s as &Trait;
+        do bh.iter {
+            t.method();
+        }
+    }
+
+    #[bench]
+    fn trait_static_method_call(bh: &mut BenchHarness) {
+        let s = Struct { field: 10 };
+        do bh.iter {
+            s.method();
+        }
+    }
+
+    // Overhead of various match forms
+
+    #[bench]
+    fn match_option_some(bh: &mut BenchHarness) {
+        let x = Some(10);
+        do bh.iter {
+            let _q = match x {
+                Some(y) => y,
+                None => 11
+            };
+        }
+    }
+
+    #[bench]
+    fn match_vec_pattern(bh: &mut BenchHarness) {
+        let x = [1,2,3,4,5,6];
+        do bh.iter {
+            let _q = match x {
+                [1,2,3,.._] => 10,
+                _ => 11
+            };
+        }
+    }
+}