|
| 1 | +#![feature(test)] |
| 2 | +extern crate parallel_letter_frequency; |
| 3 | +extern crate test; |
| 4 | + |
| 5 | +use std::collections::HashMap; |
| 6 | +use test::Bencher; |
| 7 | + |
| 8 | +#[bench] |
| 9 | +fn bench_tiny_parallel(b: &mut Bencher) { |
| 10 | + let tiny = &["a"]; |
| 11 | + b.iter(|| parallel_letter_frequency::frequency(tiny, 3)); |
| 12 | +} |
| 13 | + |
| 14 | +#[bench] |
| 15 | +fn bench_tiny_sequential(b: &mut Bencher) { |
| 16 | + let tiny = &["a"]; |
| 17 | + b.iter(|| frequency(tiny)); |
| 18 | +} |
| 19 | + |
| 20 | +#[bench] |
| 21 | +fn bench_small_parallel(b: &mut Bencher) { |
| 22 | + let texts = all_texts(1); |
| 23 | + b.iter(|| parallel_letter_frequency::frequency(&texts, 3)); |
| 24 | +} |
| 25 | + |
| 26 | +#[bench] |
| 27 | +fn bench_small_sequential(b: &mut Bencher) { |
| 28 | + let texts = all_texts(1); |
| 29 | + b.iter(|| frequency(&texts)); |
| 30 | +} |
| 31 | + |
| 32 | +#[bench] |
| 33 | +fn bench_large_parallel(b: &mut Bencher) { |
| 34 | + let texts = all_texts(30); |
| 35 | + b.iter(|| parallel_letter_frequency::frequency(&texts, 3)); |
| 36 | +} |
| 37 | + |
| 38 | +#[bench] |
| 39 | +fn bench_large_sequential(b: &mut Bencher) { |
| 40 | + let texts = all_texts(30); |
| 41 | + b.iter(|| frequency(&texts)); |
| 42 | +} |
| 43 | + |
| 44 | +/// Simple sequential char frequency. Can it be beat? |
| 45 | +pub fn frequency(texts: &[&str]) -> HashMap<char, usize> { |
| 46 | + let mut map = HashMap::new(); |
| 47 | + |
| 48 | + for line in texts { |
| 49 | + for chr in line.chars().filter(|c| c.is_alphabetic()) { |
| 50 | + if let Some(c) = chr.to_lowercase().next() { |
| 51 | + (*map.entry(c).or_insert(0)) += 1; |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + map |
| 57 | +} |
| 58 | + |
| 59 | +fn all_texts(repeat: usize) -> Vec<&'static str> { |
| 60 | + [ODE_AN_DIE_FREUDE, WILHELMUS, STAR_SPANGLED_BANNER] |
| 61 | + .iter() |
| 62 | + .cycle() |
| 63 | + .take(3 * repeat) |
| 64 | + .flat_map(|anthem| anthem.iter().cloned()) |
| 65 | + .collect() |
| 66 | +} |
| 67 | + |
| 68 | +// Poem by Friedrich Schiller. The corresponding music is the European Anthem. |
| 69 | +pub const ODE_AN_DIE_FREUDE: [&'static str; 8] = [ |
| 70 | + "Freude schöner Götterfunken", |
| 71 | + "Tochter aus Elysium,", |
| 72 | + "Wir betreten feuertrunken,", |
| 73 | + "Himmlische, dein Heiligtum!", |
| 74 | + "Deine Zauber binden wieder", |
| 75 | + "Was die Mode streng geteilt;", |
| 76 | + "Alle Menschen werden Brüder,", |
| 77 | + "Wo dein sanfter Flügel weilt."]; |
| 78 | + |
| 79 | +// Dutch national anthem |
| 80 | +pub const WILHELMUS: [&'static str; 8] = [ |
| 81 | + "Wilhelmus van Nassouwe", |
| 82 | + "ben ik, van Duitsen bloed,", |
| 83 | + "den vaderland getrouwe", |
| 84 | + "blijf ik tot in den dood.", |
| 85 | + "Een Prinse van Oranje", |
| 86 | + "ben ik, vrij, onverveerd,", |
| 87 | + "den Koning van Hispanje", |
| 88 | + "heb ik altijd geëerd."]; |
| 89 | + |
| 90 | +// American national anthem |
| 91 | +pub const STAR_SPANGLED_BANNER: [&'static str; 8] = [ |
| 92 | + "O say can you see by the dawn's early light,", |
| 93 | + "What so proudly we hailed at the twilight's last gleaming,", |
| 94 | + "Whose broad stripes and bright stars through the perilous fight,", |
| 95 | + "O'er the ramparts we watched, were so gallantly streaming?", |
| 96 | + "And the rockets' red glare, the bombs bursting in air,", |
| 97 | + "Gave proof through the night that our flag was still there;", |
| 98 | + "O say does that star-spangled banner yet wave,", |
| 99 | + "O'er the land of the free and the home of the brave?"]; |
0 commit comments