Improve benches for the word functions

main
John Zacarias Jekel 1 year ago
parent 46b24494e8
commit 3b7e6b85a8
  1. 2
      src/anslatortray.rs
  2. 2
      src/translate_strings.rs
  3. 239
      src/translate_words.rs

@ -90,6 +90,7 @@ fn interactive(args: &Vec<String>) {
eprint!("anslatortray> ");
stdin.read_line(&mut line_buffer).unwrap();
eprintln!("{}", translate(&line_buffer));
line_buffer.truncate(0);
}
}
@ -193,5 +194,6 @@ fn stdin_to_stdout(args: &Vec<String>) {
while let Ok(bytes_read) = stdin.read_to_string(&mut buffer) {
if bytes_read == 0 { return; }
write!(stdout, "{}", translate(&buffer)).unwrap();//TODO do this more efficiently (avoid format string)
buffer.truncate(0);//TODO is this needed here?
}
}

@ -234,8 +234,6 @@ pub fn translate_with_style(english: &str, suffix_lower: &str, special_case_suff
special_case_suffix_upper.push(letter.to_ascii_uppercase());
}
eprintln!("{} -> {}, {} -> {}", suffix_lower, suffix_upper, special_case_suffix_lower, special_case_suffix_upper);
let mut pig_latin_string = String::with_capacity(english.len() * 2);//Plenty of headroom in case the words are very small or the suffixes are long
let mut current_word = String::with_capacity(64);//Longer than all English words to avoid unneeded allocations (plus leaving room for leading and trailing extra characters)
let mut contraction_suffix = String::with_capacity(64);

@ -190,48 +190,6 @@ pub(crate) fn translate_word_with_style_reuse_buffers_ascii (
/* Tests */
#[cfg(test)]
fn translate_word_with_style(english_word: &str, suffix_lower: &str, special_case_suffix_lower: &str) -> String {
let mut suffix_upper = String::with_capacity(suffix_lower.len());
for letter in suffix_lower.chars() {
suffix_upper.push(letter.to_ascii_uppercase());
}
let mut special_case_suffix_upper = String::with_capacity(special_case_suffix_lower.len());
for letter in special_case_suffix_lower.chars() {
special_case_suffix_upper.push(letter.to_ascii_uppercase());
}
let mut pig_latin_word = String::with_capacity(64 * 2);//Longer than all English words to avoid unneeded allocations, times 2 to leave room for whitespace, symbols, and the suffix
let mut starting_consonants_buffer = String::with_capacity(64);//Longer than basically all English words to avoid unneeded allocations, plus the fact that this isn't the whole word
translate_word_with_style_reuse_buffers (
english_word,
suffix_lower, special_case_suffix_lower, &suffix_upper, &special_case_suffix_upper,
&mut pig_latin_word, &mut starting_consonants_buffer
);
return pig_latin_word;
}
#[cfg(test)]
fn translate_word_with_style_ascii(english_word: &str, suffix_lower: &str, special_case_suffix_lower: &str) -> String {
let mut suffix_upper = String::with_capacity(suffix_lower.len());
for letter in suffix_lower.chars() {
suffix_upper.push(letter.to_ascii_uppercase());
}
let mut special_case_suffix_upper = String::with_capacity(special_case_suffix_lower.len());
for letter in special_case_suffix_lower.chars() {
special_case_suffix_upper.push(letter.to_ascii_uppercase());
}
let mut pig_latin_word = String::with_capacity(64 * 2);//Longer than all English words to avoid unneeded allocations, times 2 to leave room for whitespace, symbols, and the suffix
let mut starting_consonants_buffer = String::with_capacity(64);//Longer than basically all English words to avoid unneeded allocations, plus the fact that this isn't the whole word
translate_word_with_style_reuse_buffers_ascii (
english_word,
suffix_lower, special_case_suffix_lower, &suffix_upper, &special_case_suffix_upper,
&mut pig_latin_word, &mut starting_consonants_buffer
);
return pig_latin_word;
}
#[cfg(test)]
mod tests {
use super::*;
@ -311,6 +269,46 @@ mod tests {
assert_eq!(translate_word_with_style_ascii("nice", suffix, special_case_suffix), "icen".to_string() + suffix);
}
}
fn translate_word_with_style(english_word: &str, suffix_lower: &str, special_case_suffix_lower: &str) -> String {
let mut suffix_upper = String::with_capacity(suffix_lower.len());
for letter in suffix_lower.chars() {
suffix_upper.push(letter.to_ascii_uppercase());
}
let mut special_case_suffix_upper = String::with_capacity(special_case_suffix_lower.len());
for letter in special_case_suffix_lower.chars() {
special_case_suffix_upper.push(letter.to_ascii_uppercase());
}
let mut pig_latin_word = String::with_capacity(64 * 2);//Longer than all English words to avoid unneeded allocations, times 2 to leave room for whitespace, symbols, and the suffix
let mut starting_consonants_buffer = String::with_capacity(64);//Longer than basically all English words to avoid unneeded allocations, plus the fact that this isn't the whole word
translate_word_with_style_reuse_buffers (
english_word,
suffix_lower, special_case_suffix_lower, &suffix_upper, &special_case_suffix_upper,
&mut pig_latin_word, &mut starting_consonants_buffer
);
return pig_latin_word;
}
fn translate_word_with_style_ascii(english_word: &str, suffix_lower: &str, special_case_suffix_lower: &str) -> String {
let mut suffix_upper = String::with_capacity(suffix_lower.len());
for letter in suffix_lower.chars() {
suffix_upper.push(letter.to_ascii_uppercase());
}
let mut special_case_suffix_upper = String::with_capacity(special_case_suffix_lower.len());
for letter in special_case_suffix_lower.chars() {
special_case_suffix_upper.push(letter.to_ascii_uppercase());
}
let mut pig_latin_word = String::with_capacity(64 * 2);//Longer than all English words to avoid unneeded allocations, times 2 to leave room for whitespace, symbols, and the suffix
let mut starting_consonants_buffer = String::with_capacity(64);//Longer than basically all English words to avoid unneeded allocations, plus the fact that this isn't the whole word
translate_word_with_style_reuse_buffers_ascii (
english_word,
suffix_lower, special_case_suffix_lower, &suffix_upper, &special_case_suffix_upper,
&mut pig_latin_word, &mut starting_consonants_buffer
);
return pig_latin_word;
}
}
/* Benches */
@ -323,23 +321,162 @@ mod benches {
use super::*;
#[bench]
fn translate_word_the_word_translator(b: &mut Bencher) {
b.iter(|| -> String {
return translate_word_with_style("translator", "ay", "way");
fn way_the_word_translator(b: &mut Bencher) {
let mut pig_latin_word = String::with_capacity(64 * 2);//Longer than all English words to avoid unneeded allocations, times 2 to leave room for whitespace, symbols, and the suffix
let mut starting_consonants_buffer = String::with_capacity(64);//Longer than basically all English words to avoid unneeded allocations, plus the fact that this isn't the whole word
b.iter(|| {
let word = test::black_box("translator");
translate_word_with_style_reuse_buffers (
word,
"ay", "way", "AY", "WAY",
&mut pig_latin_word, &mut starting_consonants_buffer
);
pig_latin_word.truncate(0);
});
eprintln!("{}", pig_latin_word);//To avoid optimizing things out
}
#[bench]
fn yay_the_word_translator(b: &mut Bencher) {
let mut pig_latin_word = String::with_capacity(64 * 2);//Longer than all English words to avoid unneeded allocations, times 2 to leave room for whitespace, symbols, and the suffix
let mut starting_consonants_buffer = String::with_capacity(64);//Longer than basically all English words to avoid unneeded allocations, plus the fact that this isn't the whole word
b.iter(|| {
let word = test::black_box("translator");
translate_word_with_style_reuse_buffers (
word,
"ay", "yay", "AY", "YAY",
&mut pig_latin_word, &mut starting_consonants_buffer
);
pig_latin_word.truncate(0);
});
eprintln!("{}", pig_latin_word);//To avoid optimizing things out
}
#[bench]
fn hay_the_word_translator(b: &mut Bencher) {
let mut pig_latin_word = String::with_capacity(64 * 2);//Longer than all English words to avoid unneeded allocations, times 2 to leave room for whitespace, symbols, and the suffix
let mut starting_consonants_buffer = String::with_capacity(64);//Longer than basically all English words to avoid unneeded allocations, plus the fact that this isn't the whole word
b.iter(|| {
let word = test::black_box("translator");
translate_word_with_style_reuse_buffers (
word,
"ay", "hay", "AY", "HAY",
&mut pig_latin_word, &mut starting_consonants_buffer
);
pig_latin_word.truncate(0);
});
eprintln!("{}", pig_latin_word);//To avoid optimizing things out
}
#[bench]
fn translate_word_yay_the_word_translator(b: &mut Bencher) {
b.iter(|| -> String {
return translate_word_with_style("translator", "ay", "yay");
fn ferb_the_word_translator(b: &mut Bencher) {
let mut pig_latin_word = String::with_capacity(64 * 2);//Longer than all English words to avoid unneeded allocations, times 2 to leave room for whitespace, symbols, and the suffix
let mut starting_consonants_buffer = String::with_capacity(64);//Longer than basically all English words to avoid unneeded allocations, plus the fact that this isn't the whole word
b.iter(|| {
let word = test::black_box("translator");
translate_word_with_style_reuse_buffers (
word,
"erb", "ferb", "ERB", "FERB",
&mut pig_latin_word, &mut starting_consonants_buffer
);
pig_latin_word.truncate(0);
});
eprintln!("{}", pig_latin_word);//To avoid optimizing things out
}
#[bench]
fn translate_word_ferb_the_word_translator(b: &mut Bencher) {
b.iter(|| -> String {
return translate_word_with_style("translator", "erb", "ferb");
fn ascii_way_the_word_translator(b: &mut Bencher) {
let mut pig_latin_word = String::with_capacity(64 * 2);//Longer than all English words to avoid unneeded allocations, times 2 to leave room for whitespace, symbols, and the suffix
let mut starting_consonants_buffer = String::with_capacity(64);//Longer than basically all English words to avoid unneeded allocations, plus the fact that this isn't the whole word
b.iter(|| {
let word = test::black_box("translator");
translate_word_with_style_reuse_buffers_ascii (
word,
"ay", "way", "AY", "WAY",
&mut pig_latin_word, &mut starting_consonants_buffer
);
pig_latin_word.truncate(0);
});
eprintln!("{}", pig_latin_word);//To avoid optimizing things out
}
#[bench]
fn ascii_yay_the_word_translator(b: &mut Bencher) {
let mut pig_latin_word = String::with_capacity(64 * 2);//Longer than all English words to avoid unneeded allocations, times 2 to leave room for whitespace, symbols, and the suffix
let mut starting_consonants_buffer = String::with_capacity(64);//Longer than basically all English words to avoid unneeded allocations, plus the fact that this isn't the whole word
b.iter(|| {
let word = test::black_box("translator");
translate_word_with_style_reuse_buffers_ascii (
word,
"ay", "yay", "AY", "YAY",
&mut pig_latin_word, &mut starting_consonants_buffer
);
pig_latin_word.truncate(0);
});
eprintln!("{}", pig_latin_word);//To avoid optimizing things out
}
#[bench]
fn ascii_hay_the_word_translator(b: &mut Bencher) {
let mut pig_latin_word = String::with_capacity(64 * 2);//Longer than all English words to avoid unneeded allocations, times 2 to leave room for whitespace, symbols, and the suffix
let mut starting_consonants_buffer = String::with_capacity(64);//Longer than basically all English words to avoid unneeded allocations, plus the fact that this isn't the whole word
b.iter(|| {
let word = test::black_box("translator");
translate_word_with_style_reuse_buffers_ascii (
word,
"ay", "hay", "AY", "HAY",
&mut pig_latin_word, &mut starting_consonants_buffer
);
pig_latin_word.truncate(0);
});
eprintln!("{}", pig_latin_word);//To avoid optimizing things out
}
#[bench]
fn ascii_ferb_the_word_translator(b: &mut Bencher) {
let mut pig_latin_word = String::with_capacity(64 * 2);//Longer than all English words to avoid unneeded allocations, times 2 to leave room for whitespace, symbols, and the suffix
let mut starting_consonants_buffer = String::with_capacity(64);//Longer than basically all English words to avoid unneeded allocations, plus the fact that this isn't the whole word
b.iter(|| {
let word = test::black_box("translator");
translate_word_with_style_reuse_buffers_ascii (
word,
"erb", "ferb", "ERB", "FERB",
&mut pig_latin_word, &mut starting_consonants_buffer
);
pig_latin_word.truncate(0);
});
eprintln!("{}", pig_latin_word);//To avoid optimizing things out
}
}

Loading…
Cancel
Save