Last active
April 13, 2018 15:41
-
-
Save starfys/56c2d355b42c12a2c5731b5e5fc9287a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![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