Debian Codegraph @

debian/missing-sources/tendril/src/bench.rs

raw
1 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
2 // https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
3 // <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
4 // option. This file may not be copied, modified, or distributed
5 // except according to those terms.
6
7 use std::borrow::ToOwned;
8 use std::collections::hash_map::{Entry, HashMap};
9
10 use tendril::StrTendril;
11
12 fn index_words_string(input: &String) -> HashMap<char, Vec<String>> {
13 let mut index = HashMap::new();
14 for word in input.split(|c| c == ' ') {
15 if word.len() == 0 {
16 continue;
17 }
18 let word = word.to_owned();
19 match index.entry(word.chars().next().unwrap()) {
20 Entry::Occupied(mut e) => {
21 let x: &mut Vec<String> = e.get_mut();
22 x.push(word);
23 }
24 Entry::Vacant(e) => {
25 e.insert(vec![word]);
26 }
27 }
28 }
29 index
30 }
31
32 fn index_words_tendril(input: &StrTendril) -> HashMap<char, Vec<StrTendril>> {
33 let mut index = HashMap::new();
34 let mut t = input.clone();
35 loop {
36 match t.pop_front_char_run(|c| c != ' ') {
37 None => return index,
38 Some((_, false)) => (),
39 Some((word, true)) => match index.entry(word.chars().next().unwrap()) {
40 Entry::Occupied(mut e) => {
41 e.get_mut().push(word);
42 }
43 Entry::Vacant(e) => {
44 e.insert(vec![word]);
45 }
46 },
47 }
48 }
49 }
50
51 static EN_1: &'static str = "Days turn to nights turn to paper into rocks into plastic";
52
53 static EN_2: &'static str =
54 "Here the notes in my laboratory journal cease. I was able to write the last \
55 words only with great effort. By now it was already clear to me that LSD had \
56 been the cause of the remarkable experience of the previous Friday, for the \
57 altered perceptions were of the same type as before, only much more intense. I \
58 had to struggle to speak intelligibly. I asked my laboratory assistant, who was \
59 informed of the self-experiment, to escort me home. We went by bicycle, no \
60 automobile being available because of wartime restrictions on their use. On the \
61 way home, my condition began to assume threatening forms. Everything in my \
62 field of vision wavered and was distorted as if seen in a curved mirror. I also \
63 had the sensation of being unable to move from the spot. Nevertheless, my \
64 assistant later told me that we had traveled very rapidly. Finally, we arrived \
65 at home safe and sound, and I was just barely capable of asking my companion to \
66 summon our family doctor and request milk from the neighbors.\n\n\
67 In spite of my delirious, bewildered condition, I had brief periods of clear \
68 and effective thinking—and chose milk as a nonspecific antidote for poisoning.";
69
70 static KR_1: &'static str =
71 "러스트(Rust)는 모질라(mozilla.org)에서 개발하고 있는, 메모리-안전하고 병렬 \
72 프로그래밍이 쉬운 차세대 프로그래밍 언어입니다. 아직 \
73 개발 단계이며 많은 기능이 구현 중으로, MIT/Apache2 라이선스로 배포됩니다.";
74
75 static HTML_KR_1: &'static str =
76 "<p>러스트(<a href=\"http://rust-lang.org\">Rust</a>)는 모질라(<a href=\"\
77 https://www.mozilla.org/\">mozilla.org</a>)에서 개발하고 있는, \
78 메모리-안전하고 병렬 프로그래밍이 쉬운 차세대 프로그래밍 언어입니다. \
79 아직 개발 단계이며 많은 기능이 구현 중으로, MIT/Apache2 라이선스로 배포됩니다.</p>";
80
81 mod index_words {
82 macro_rules! bench {
83 ($txt:ident) => {
84 #[allow(non_snake_case)]
85 mod $txt {
86 const SMALL_SIZE: usize = 65536;
87 const LARGE_SIZE: usize = (1 << 20);
88
89 #[bench]
90 fn index_words_string(b: &mut ::test::Bencher) {
91 let mut s = String::new();
92 while s.len() < SMALL_SIZE {
93 s.push_str(::tendril::bench::$txt);
94 }
95 b.iter(|| ::tendril::bench::index_words_string(&s));
96 }
97
98 #[bench]
99 fn index_words_tendril(b: &mut ::test::Bencher) {
100 let mut t = ::tendril::StrTendril::new();
101 while t.len() < SMALL_SIZE {
102 t.push_slice(::tendril::bench::$txt);
103 }
104 b.iter(|| ::tendril::bench::index_words_tendril(&t));
105 }
106
107 #[bench]
108 fn index_words_big_string(b: &mut ::test::Bencher) {
109 let mut s = String::new();
110 while s.len() < LARGE_SIZE {
111 s.push_str(::tendril::bench::$txt);
112 }
113 b.iter(|| ::tendril::bench::index_words_string(&s));
114 }
115
116 #[bench]
117 fn index_words_big_tendril(b: &mut ::test::Bencher) {
118 let mut t = ::tendril::StrTendril::new();
119 while t.len() < LARGE_SIZE {
120 t.push_slice(::tendril::bench::$txt);
121 }
122 b.iter(|| ::tendril::bench::index_words_tendril(&t));
123 }
124
125 #[test]
126 fn correctness() {
127 use std::borrow::ToOwned;
128 use tendril::bench::{index_words_string, index_words_tendril};
129 use tendril::SliceExt;
130
131 let txt = ::tendril::bench::$txt;
132 let input_string = txt.to_owned();
133 let count_s = index_words_string(&input_string);
134 let mut keys: Vec<char> = count_s.keys().cloned().collect();
135 keys.sort();
136
137 let input_tendril = txt.to_tendril();
138 let count_t = index_words_tendril(&input_tendril);
139 let mut keys_t: Vec<char> = count_t.keys().cloned().collect();
140 keys_t.sort();
141
142 assert_eq!(keys, keys_t);
143
144 for k in &keys {
145 let vs = &count_s[k];
146 let vt = &count_t[k];
147 assert_eq!(vs.len(), vt.len());
148 assert!(vs.iter().zip(vt.iter()).all(|(s, t)| **s == **t));
149 }
150 }
151 }
152 };
153 }
154
155 bench!(EN_1);
156 bench!(EN_2);
157 bench!(KR_1);
158 bench!(HTML_KR_1);
159 }