Skip to content

Commit 78053df

Browse files
committed
Benchmark parallel-letter-frequency. Closes exercism#244
1 parent 3bf514e commit 78053df

File tree

3 files changed

+88
-2
lines changed

3 files changed

+88
-2
lines changed

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ env:
1313
- DENYWARNINGS=
1414
- DENYWARNINGS=1
1515
matrix:
16+
include:
17+
- rust: nightly
18+
env: BENCHMARK=1
19+
script: "./_test/check-exercises.sh"
1620
allow_failures:
1721
- rust: nightly
1822
- rust: beta

_test/check-exercises.sh

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,20 @@ if [ -z "$DENYWARNINGS" ]; then
66
set -e
77
fi
88

9+
if [ -n "$BENCHMARK" ]; then
10+
files=exercises/*/benches
11+
else
12+
files=exercises/*/tests
13+
fi
14+
915
tmp=${TMPDIR:-/tmp/}
1016
mkdir "${tmp}exercises"
1117

1218
exitcode=0
1319

1420
# An exercise worth testing is defined here as any top level directory with
1521
# a 'tests' directory
16-
for exercise in exercises/*/tests; do
22+
for exercise in $files; do
1723
# This assumes that exercises are only one directory deep
1824
# and that the primary module is named the same as the directory
1925
directory=$(dirname "${exercise}");
@@ -38,7 +44,10 @@ for exercise in exercises/*/tests; do
3844
sed -i '/\[ignore\]/d' $test
3945
done
4046

41-
if [ -n "$DENYWARNINGS" ]; then
47+
# Run benchmarks in instead when enabled.
48+
if [ -n "$BENCHMARK" ]; then
49+
cargo bench
50+
elif [ -n "$DENYWARNINGS" ]; then
4251
sed -i -e '1i #![deny(warnings)]' src/lib.rs
4352

4453
# No-run mode so we see no test output.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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_sequential(b: &mut Bencher) {
10+
let texts = all_texts();
11+
b.iter(|| frequency(&texts));
12+
}
13+
14+
#[bench]
15+
fn bench_parallel(b: &mut Bencher) {
16+
let texts = all_texts();
17+
b.iter(|| parallel_letter_frequency::frequency(&texts, 3));
18+
}
19+
20+
/// Simple sequential char frequency. Can it be beat?
21+
pub fn frequency(texts: &[&str]) -> HashMap<char, usize> {
22+
let mut map = HashMap::new();
23+
24+
for line in texts {
25+
for chr in line.chars().filter(|c| c.is_alphabetic()) {
26+
if let Some(c) = chr.to_lowercase().next() {
27+
(*map.entry(c).or_insert(0)) += 1;
28+
}
29+
}
30+
}
31+
32+
map
33+
}
34+
35+
fn all_texts() -> Vec<&'static str> {
36+
[ODE_AN_DIE_FREUDE, WILHELMUS, STAR_SPANGLED_BANNER]
37+
.iter()
38+
.flat_map(|anthem| anthem.iter().cloned())
39+
.collect()
40+
}
41+
42+
// Poem by Friedrich Schiller. The corresponding music is the European Anthem.
43+
pub const ODE_AN_DIE_FREUDE: [&'static str; 8] = [
44+
"Freude schöner Götterfunken",
45+
"Tochter aus Elysium,",
46+
"Wir betreten feuertrunken,",
47+
"Himmlische, dein Heiligtum!",
48+
"Deine Zauber binden wieder",
49+
"Was die Mode streng geteilt;",
50+
"Alle Menschen werden Brüder,",
51+
"Wo dein sanfter Flügel weilt."];
52+
53+
// Dutch national anthem
54+
pub const WILHELMUS: [&'static str; 8] = [
55+
"Wilhelmus van Nassouwe",
56+
"ben ik, van Duitsen bloed,",
57+
"den vaderland getrouwe",
58+
"blijf ik tot in den dood.",
59+
"Een Prinse van Oranje",
60+
"ben ik, vrij, onverveerd,",
61+
"den Koning van Hispanje",
62+
"heb ik altijd geëerd."];
63+
64+
// American national anthem
65+
pub const STAR_SPANGLED_BANNER: [&'static str; 8] = [
66+
"O say can you see by the dawn's early light,",
67+
"What so proudly we hailed at the twilight's last gleaming,",
68+
"Whose broad stripes and bright stars through the perilous fight,",
69+
"O'er the ramparts we watched, were so gallantly streaming?",
70+
"And the rockets' red glare, the bombs bursting in air,",
71+
"Gave proof through the night that our flag was still there;",
72+
"O say does that star-spangled banner yet wave,",
73+
"O'er the land of the free and the home of the brave?"];

0 commit comments

Comments
 (0)