@@ -7,15 +7,21 @@ enum Cache {
7
7
L3 ,
8
8
}
9
9
10
+ impl Cache {
11
+ fn size ( & self ) -> usize {
12
+ match self {
13
+ Cache :: L1 => 1000 , // 8kb
14
+ Cache :: L2 => 10_000 , // 80kb
15
+ Cache :: L3 => 1_000_000 , // 8Mb
16
+ }
17
+ }
18
+ }
19
+
10
20
fn binary_search < F > ( b : & mut Bencher , cache : Cache , mapper : F )
11
21
where
12
22
F : Fn ( usize ) -> usize ,
13
23
{
14
- let size = match cache {
15
- Cache :: L1 => 1000 , // 8kb
16
- Cache :: L2 => 10_000 , // 80kb
17
- Cache :: L3 => 1_000_000 , // 8Mb
18
- } ;
24
+ let size = cache. size ( ) ;
19
25
let v = ( 0 ..size) . map ( & mapper) . collect :: < Vec < _ > > ( ) ;
20
26
let mut r = 0usize ;
21
27
b. iter ( move || {
24
30
// Lookup the whole range to get 50% hits and 50% misses.
25
31
let i = mapper ( r % size) ;
26
32
black_box ( v. binary_search ( & i) . is_ok ( ) ) ;
27
- } )
33
+ } ) ;
34
+ }
35
+
36
+ fn binary_search_worst_case ( b : & mut Bencher , cache : Cache ) {
37
+ let size = cache. size ( ) ;
38
+
39
+ let mut v = vec ! [ 0 ; size] ;
40
+ let i = 1 ;
41
+ v[ size - 1 ] = i;
42
+ b. iter ( move || {
43
+ black_box ( v. binary_search ( & i) . is_ok ( ) ) ;
44
+ } ) ;
28
45
}
29
46
30
47
#[ bench]
@@ -57,6 +74,21 @@ fn binary_search_l3_with_dups(b: &mut Bencher) {
57
74
binary_search ( b, Cache :: L3 , |i| i / 16 * 16 ) ;
58
75
}
59
76
77
+ #[ bench]
78
+ fn binary_search_l1_worst_case ( b : & mut Bencher ) {
79
+ binary_search_worst_case ( b, Cache :: L1 ) ;
80
+ }
81
+
82
+ #[ bench]
83
+ fn binary_search_l2_worst_case ( b : & mut Bencher ) {
84
+ binary_search_worst_case ( b, Cache :: L2 ) ;
85
+ }
86
+
87
+ #[ bench]
88
+ fn binary_search_l3_worst_case ( b : & mut Bencher ) {
89
+ binary_search_worst_case ( b, Cache :: L3 ) ;
90
+ }
91
+
60
92
macro_rules! rotate {
61
93
( $fn: ident, $n: expr, $mapper: expr) => {
62
94
#[ bench]
0 commit comments