-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathstrings.rs
104 lines (91 loc) · 2.3 KB
/
strings.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#![feature(test)]
#![allow(non_snake_case)]
extern crate test;
use algo::common;
use algo::strings::{brute_force, Quick3String, KMP, LSD, MSD};
use test::Bencher;
const WORDS3: &'static str = include_str!("../res/strings/words3.txt");
const SHELLS: &'static str = include_str!("../res/strings/shells.txt");
#[bench]
fn sort_str_std_Vec(b: &mut Bencher) {
let i = WORDS3;
let mut words = extract_words(i);
b.iter(|| {
words.sort();
});
}
#[bench]
fn sort_i32_std_Vec(b: &mut Bencher) {
let mut nums: Vec<i32> = (0..1000).rev().collect();
b.iter(|| {
nums.sort();
});
}
#[bench]
fn sort_str_LSD_radix(b: &mut Bencher) {
let i = WORDS3;
let mut words = extract_words(i);
let w = words[0].len();
b.iter(|| {
LSD::sort(&mut words, w);
});
}
#[bench]
fn sort_i32_LSD_radix(b: &mut Bencher) {
let mut nums: Vec<i32> = (0..1000).rev().collect();
b.iter(|| {
LSD::sort_i32(&mut nums);
});
}
#[bench]
fn sort_str_MSD_radix(b: &mut Bencher) {
let i = SHELLS;
let mut words = extract_words(i);
b.iter(|| {
MSD::sort(&mut words);
});
}
#[bench]
fn sort_str_quick3strings(b: &mut Bencher) {
let i = SHELLS;
let mut words = extract_words(i);
b.iter(|| {
Quick3String::sort(&mut words);
});
}
#[bench]
fn MSD_worst_case(b: &mut Bencher) {
// examines just 1 char to distinguish among the keys
let mut words = vec!["1DNB377"; 26];
b.iter(|| {
MSD::sort(&mut words);
});
}
#[bench]
fn MSD_best_case(b: &mut Bencher) {
// all strings equal, need check all chars
let words = common::util::vec_alphabet("1DNB377".len());
let mut words: Vec<&str> = words.iter().map(|it| it.as_str()).collect();
b.iter(|| {
MSD::sort(&mut words);
});
}
#[bench]
fn sub_search_kmp(b: &mut Bencher) {
let mut pat = "A".repeat(10);
pat.push('B');
let txt = "A".repeat(10000);
let kmp = KMP::from(pat.as_str());
b.iter(|| kmp.search(txt.as_str()));
}
#[bench]
fn sub_search_brute_force(b: &mut Bencher) {
// worst case for brute force search
let mut pat = "A".repeat(10);
pat.push('B');
let txt = "A".repeat(10000);
b.iter(|| brute_force::search1(pat.as_str(), txt.as_str()));
}
fn extract_words(i: &str) -> Vec<&str> {
i.split_whitespace().collect()
}