Skip to content

Commit fb893a1

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

File tree

9 files changed

+110
-33
lines changed

9 files changed

+110
-33
lines changed

.travis.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ sudo: false
88
rust:
99
- stable
1010
- beta
11-
- nightly
1211
env:
1312
- DENYWARNINGS=
1413
- DENYWARNINGS=1
1514
matrix:
15+
include:
16+
- rust: nightly
17+
env: BENCHMARK=1
1618
allow_failures:
1719
- rust: nightly
1820
- rust: beta

_test/check-exercises.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ for exercise in exercises/*/tests; do
5151
# Run the test and get the status
5252
cargo test
5353
fi
54+
55+
# Run benchmarks if present.
56+
if [ -n "$BENCHMARK" ] && [ -d benches ]; then
57+
cargo bench
58+
fi
5459
)
5560

5661
status=$?

exercises/parallel-letter-frequency/Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
[package]
22
name = "parallel-letter-frequency"
33
version = "0.0.0"
4+
5+
[dev-dependencies]
6+
sequential-letter-frequency = { path = "sequential-letter-frequency" }
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#![feature(test)]
2+
extern crate parallel_letter_frequency;
3+
extern crate sequential_letter_frequency;
4+
extern crate test;
5+
6+
use sequential_letter_frequency::*;
7+
use test::Bencher;
8+
9+
#[bench]
10+
fn bench_sequential(b: &mut Bencher) {
11+
b.iter(|| sequential_letter_frequency::frequency(&all_texts()));
12+
}
13+
14+
#[bench]
15+
fn bench_parallel(b: &mut Bencher) {
16+
b.iter(|| parallel_letter_frequency::frequency(&all_texts(), 3));
17+
}
18+
19+
fn all_texts() -> Vec<&'static str> {
20+
[ODE_AN_DIE_FREUDE, WILHELMUS, STAR_SPANGLED_BANNER]
21+
.iter()
22+
.flat_map(|anthem| anthem.iter().cloned())
23+
.collect()
24+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
!**src
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[package]
2+
name = "sequential-letter-frequency"
3+
version = "0.0.0"
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use std::collections::HashMap;
2+
3+
/// Simple sequential char frequency. Can it be beat?
4+
pub fn frequency(texts: &[&str]) -> HashMap<char, usize> {
5+
let mut map = HashMap::new();
6+
7+
for line in texts {
8+
for chr in line.chars().filter(|c| c.is_alphabetic()) {
9+
if let Some(c) = chr.to_lowercase().next() {
10+
(*map.entry(c).or_insert(0)) += 1;
11+
}
12+
}
13+
}
14+
15+
map
16+
}
17+
18+
// Poem by Friedrich Schiller. The corresponding music is the European Anthem.
19+
pub const ODE_AN_DIE_FREUDE: [&'static str; 8] = [
20+
"Freude schöner Götterfunken",
21+
"Tochter aus Elysium,",
22+
"Wir betreten feuertrunken,",
23+
"Himmlische, dein Heiligtum!",
24+
"Deine Zauber binden wieder",
25+
"Was die Mode streng geteilt;",
26+
"Alle Menschen werden Brüder,",
27+
"Wo dein sanfter Flügel weilt."];
28+
29+
// Dutch national anthem
30+
pub const WILHELMUS: [&'static str; 8] = [
31+
"Wilhelmus van Nassouwe",
32+
"ben ik, van Duitsen bloed,",
33+
"den vaderland getrouwe",
34+
"blijf ik tot in den dood.",
35+
"Een Prinse van Oranje",
36+
"ben ik, vrij, onverveerd,",
37+
"den Koning van Hispanje",
38+
"heb ik altijd geëerd."];
39+
40+
// American national anthem
41+
pub const STAR_SPANGLED_BANNER: [&'static str; 8] = [
42+
"O say can you see by the dawn's early light,",
43+
"What so proudly we hailed at the twilight's last gleaming,",
44+
"Whose broad stripes and bright stars through the perilous fight,",
45+
"O'er the ramparts we watched, were so gallantly streaming?",
46+
"And the rockets' red glare, the bombs bursting in air,",
47+
"Gave proof through the night that our flag was still there;",
48+
"O say does that star-spangled banner yet wave,",
49+
"O'er the land of the free and the home of the brave?"];

exercises/parallel-letter-frequency/tests/parallel-letter-frequency.rs

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,9 @@
11
use std::collections::HashMap;
22

33
extern crate parallel_letter_frequency as frequency;
4+
extern crate sequential_letter_frequency;
45

5-
// Poem by Friedrich Schiller. The corresponding music is the European Anthem.
6-
const ODE_AN_DIE_FREUDE: [&'static str; 8] = [
7-
"Freude schöner Götterfunken",
8-
"Tochter aus Elysium,",
9-
"Wir betreten feuertrunken,",
10-
"Himmlische, dein Heiligtum!",
11-
"Deine Zauber binden wieder",
12-
"Was die Mode streng geteilt;",
13-
"Alle Menschen werden Brüder,",
14-
"Wo dein sanfter Flügel weilt."];
15-
16-
// Dutch national anthem
17-
const WILHELMUS: [&'static str; 8] = [
18-
"Wilhelmus van Nassouwe",
19-
"ben ik, van Duitsen bloed,",
20-
"den vaderland getrouwe",
21-
"blijf ik tot in den dood.",
22-
"Een Prinse van Oranje",
23-
"ben ik, vrij, onverveerd,",
24-
"den Koning van Hispanje",
25-
"heb ik altijd geëerd."];
26-
27-
// American national anthem
28-
const STAR_SPANGLED_BANNER: [&'static str; 8] = [
29-
"O say can you see by the dawn's early light,",
30-
"What so proudly we hailed at the twilight's last gleaming,",
31-
"Whose broad stripes and bright stars through the perilous fight,",
32-
"O'er the ramparts we watched, were so gallantly streaming?",
33-
"And the rockets' red glare, the bombs bursting in air,",
34-
"Gave proof through the night that our flag was still there;",
35-
"O say does that star-spangled banner yet wave,",
36-
"O'er the land of the free and the home of the brave?"];
6+
use sequential_letter_frequency::*;
377

388
#[test]
399
fn test_no_texts() {
@@ -121,3 +91,16 @@ fn test_all_three_anthems_3_workers() {
12191
assert_eq!(freqs.get(&'t'), Some(&56));
12292
assert_eq!(freqs.get(&'ü'), Some(&2));
12393
}
94+
95+
#[test]
96+
fn test_matches_sequential() {
97+
let mut v = Vec::new();
98+
for anthem in [ODE_AN_DIE_FREUDE, WILHELMUS, STAR_SPANGLED_BANNER].iter() {
99+
for line in anthem.iter() {
100+
v.push(*line);
101+
}
102+
}
103+
let freqs = frequency::frequency(&v[..], 3);
104+
let ref_freqs = sequential_letter_frequency::frequency(&v[..]);
105+
assert_eq!(freqs, ref_freqs);
106+
}

0 commit comments

Comments
 (0)