daily pastebin goal
1%
SHARE
TWEET

Untitled

a guest Jun 13th, 2018 51 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #![feature(test)]
  2. extern crate test;
  3.  
  4. fn main() {
  5.     println!("Hello, world!");
  6. }
  7.  
  8. #[derive(Debug, Copy, Clone, PartialEq)]
  9. pub struct Point {
  10.     pub ch: Option<char>,
  11. }
  12.  
  13. impl Point {
  14.     #[inline]
  15.     pub fn get_char(&self) -> Option<char> {
  16.         self.ch
  17.     }
  18. }
  19.  
  20. pub fn gen_word_1(rules: &[Point]) -> String {
  21.     rules
  22.         .into_iter()
  23.         .map(|r| r.get_char())
  24.         .filter_map(|r| r)
  25.         .collect()
  26. }
  27.  
  28. pub fn gen_word_2(rules: &[Point]) -> String {
  29.     let mut vec = Vec::new();
  30.  
  31.     for rule in rules.iter() {
  32.         if let Some(c) = rule.get_char() {
  33.             vec.push(c)
  34.         }
  35.     }
  36.  
  37.     vec.into_iter().collect()
  38. }
  39.  
  40. pub fn gen_word_3(rules: &[Point]) -> String {
  41.     let mut out = String::with_capacity(rules.len());
  42.     out.extend(rules.iter().filter_map(|r| r.get_char()));
  43.     out.shrink_to_fit();
  44.     out
  45. }
  46.  
  47. pub fn gen_word_4(rules: &[Point]) -> String {
  48.     rules.iter().filter_map(|r| r.get_char()).collect()
  49. }
  50.  
  51. #[cfg(test)]
  52. mod tests {
  53.     use super::*;
  54.     use test::Bencher;
  55.  
  56.     fn data() -> Vec<Point> {
  57.         (0..5000)
  58.             .map(|x| {
  59.                 let o = if x % 2 == 0 { Some('a') } else { None };
  60.                 Point { ch: o }
  61.             })
  62.             .collect()
  63.     }
  64.  
  65.     #[bench]
  66.     fn bench_gen_word_1(b: &mut Bencher) {
  67.         let words = data();
  68.         b.iter(|| gen_word_1(&words));
  69.     }
  70.  
  71.     #[bench]
  72.     fn bench_gen_word_2(b: &mut Bencher) {
  73.         let words = data();
  74.         b.iter(|| gen_word_2(&words));
  75.     }
  76.  
  77.     #[bench]
  78.     fn bench_gen_word_3(b: &mut Bencher) {
  79.         let words = data();
  80.         b.iter(|| gen_word_3(&words));
  81.     }
  82.  
  83.     #[bench]
  84.     fn bench_gen_word_4(b: &mut Bencher) {
  85.         let words = data();
  86.         b.iter(|| gen_word_4(&words));
  87.     }
  88. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top