]> git.proxmox.com Git - rustc.git/blob - src/librustc/benches/lib.rs
New upstream version 1.41.1+dfsg1
[rustc.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(|| {
29 t.method()
30 });
31 }
32
33 #[bench]
34 fn trait_static_method_call(b: &mut Bencher) {
35 let s = Struct { field: 10 };
36 b.iter(|| {
37 s.method()
38 });
39 }
40
41 // Overhead of various match forms
42
43 #[bench]
44 fn option_some(b: &mut Bencher) {
45 let x = Some(10);
46 b.iter(|| {
47 match x {
48 Some(y) => y,
49 None => 11
50 }
51 });
52 }
53
54 #[bench]
55 fn vec_pattern(b: &mut Bencher) {
56 let x = [1,2,3,4,5,6];
57 b.iter(|| {
58 match x {
59 [1,2,3,..] => 10,
60 _ => 11,
61 }
62 });
63 }