feat: add rayon

This commit is contained in:
2021-01-02 18:26:00 +08:00
parent 695561da70
commit d7488c82d7
3 changed files with 192 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
use rayon::prelude::*;
fn main() {
println!("{}", sum_of_squares(&vec![11, 22, 3, 4, 5, 6, 7]));
println!("{}", fib_iterative(40));
println!("{}", fib_recursive(40));
}
fn sum_of_squares(input: &[i32]) -> i32 {
input.par_iter() // <-- just change that!
.map(|&i| i * i)
.sum()
}
fn fib_iterative(n: u128) -> u128 {
let mut a = 0;
let mut b = 1;
for _ in 0..n {
let c = a + b;
a = b;
b = c;
}
a
}
fn fib_recursive(n: u128) -> u128 {
if n < 2 {
return n;
}
fib_recursive(n - 1) + fib_recursive(n - 2)
}