Skip to content

Instantly share code, notes, and snippets.

@starfys
Last active April 13, 2018 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save starfys/56c2d355b42c12a2c5731b5e5fc9287a to your computer and use it in GitHub Desktop.
Save starfys/56c2d355b42c12a2c5731b5e5fc9287a to your computer and use it in GitHub Desktop.
#![feature(iterator_step_by)]
extern crate rayon;
use rayon::prelude::*;
use std::time::SystemTime;
fn is_prime(n: i64) -> bool {
if n == 1 || (n > 2 && n % 2 == 0) {
false
} else {
let mut rbool = true;
let sroot: i64 = (n as f64).sqrt() as i64;
for i in (3..sroot).step_by(2) {
if n % i == 0 {
rbool = false;
break;
}
}
rbool
}
}
fn main() {
let input: Vec<i64> = (0..=1000000).collect();
let start = SystemTime::now();
for _ in 0..100 {
let output: Vec<bool> = input.par_iter().map(|n| is_prime(*n)).collect();
}
let since_the_epoch = SystemTime::now()
.duration_since(start)
.expect("Time went backwards");
println!("{:?}", since_the_epoch);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment