Advertisement
Guest User

rust benchmarks

a guest
Nov 21st, 2019
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 2.90 KB | None | 0 0
  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.  
  80. // Dutch national anthem
  81. pub const WILHELMUS: [&'static str; 8] = [
  82.    "Wilhelmus van Nassouwe",
  83.    "ben ik, van Duitsen bloed,",
  84.    "den vaderland getrouwe",
  85.    "blijf ik tot in den dood.",
  86.    "Een Prinse van Oranje",
  87.    "ben ik, vrij, onverveerd,",
  88.    "den Koning van Hispanje",
  89.    "heb ik altijd geëerd.",
  90. ];
  91.  
  92. // American national anthem
  93. pub const STAR_SPANGLED_BANNER: [&'static str; 8] = [
  94.     "O say can you see by the dawn's early light,",
  95.     "What so proudly we hailed at the twilight's last gleaming,",
  96.     "Whose broad stripes and bright stars through the perilous fight,",
  97.     "O'er the ramparts we watched, were so gallantly streaming?",
  98.     "And the rockets' red glare, the bombs bursting in air,",
  99.     "Gave proof through the night that our flag was still there;",
  100.     "O say does that star-spangled banner yet wave,",
  101.     "O'er the land of the free and the home of the brave?",
  102. ];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement