fsb4000

Rust

Feb 12th, 2020
534
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 1.17 KB | None | 0 0
  1. extern crate rand;
  2. extern crate rayon;
  3. use rand::Rng;
  4. use rand::distributions::Alphanumeric;
  5. use rayon::prelude::*;
  6.  
  7. use std::time::Instant;
  8.  
  9. fn main() {
  10.     for _ in 0..10 {
  11.         let timer = Instant::now();
  12.         let vec: Vec<String> = (0..100_000)
  13.             .into_par_iter()
  14.             .map_init(
  15.                 || rand::thread_rng(),
  16.                 |rng, _| rng.sample_iter(&Alphanumeric).take(100).collect()
  17.             ).collect();
  18.  
  19.         let overall_count: u32 =
  20.             (0..1000)
  21.                 .into_par_iter()
  22.                 .fold(|| 0u32, |acc, _| {
  23.                     let needle: String = rand::thread_rng()
  24.                         .sample_iter(&Alphanumeric)
  25.                         .take(20)
  26.                         .collect();
  27.                     if (&vec).iter().any(|s100| {
  28.                         s100.contains(&needle)
  29.                     }) {
  30.                         acc + 1
  31.                     } else {
  32.                         acc
  33.                     }
  34.                 })
  35.                 .sum();
  36.  
  37.         println!("Found {} instances of in {:.2} secs", overall_count,
  38.                  timer.elapsed().as_secs_f64());
  39.     }
  40. }
Add Comment
Please, Sign In to add comment