]> git.lizzy.rs Git - rust.git/blob - src/librustc/benches/lib.rs
Rollup merge of #68288 - RalfJung:fmt, r=oli-obk
[rust.git] / src / librustc / benches / lib.rs
1 #![feature(slice_patterns)]
2 #![feature(test)]
3
4 extern crate test;
5
6 use test::Bencher;
7
8 // Static/dynamic method dispatch
9
10 struct Struct {
11     field: isize,
12 }
13
14 trait Trait {
15     fn method(&self) -> isize;
16 }
17
18 impl Trait for Struct {
19     fn method(&self) -> isize {
20         self.field
21     }
22 }
23
24 #[bench]
25 fn trait_vtable_method_call(b: &mut Bencher) {
26     let s = Struct { field: 10 };
27     let t = &s as &dyn Trait;
28     b.iter(|| t.method());
29 }
30
31 #[bench]
32 fn trait_static_method_call(b: &mut Bencher) {
33     let s = Struct { field: 10 };
34     b.iter(|| s.method());
35 }
36
37 // Overhead of various match forms
38
39 #[bench]
40 fn option_some(b: &mut Bencher) {
41     let x = Some(10);
42     b.iter(|| match x {
43         Some(y) => y,
44         None => 11,
45     });
46 }
47
48 #[bench]
49 fn vec_pattern(b: &mut Bencher) {
50     let x = [1, 2, 3, 4, 5, 6];
51     b.iter(|| match x {
52         [1, 2, 3, ..] => 10,
53         _ => 11,
54     });
55 }