From 1158c74c9dd36f96f49ef4a7e27b27cac0096b1a Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Tue, 6 Apr 2021 11:35:48 +0300 Subject: try rayon and fail --- src/main.rs | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/src/main.rs b/src/main.rs index c67d111..22870a9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +use rayon::prelude::*; use sha2::{Digest, Sha256}; const N: usize = 10; @@ -5,43 +6,52 @@ const SIZE: usize = 128 * 1024; fn main() { println!("sequential"); - for checksum in Blobs::new(SIZE, N).map(|x| sha256(&x)) { + for checksum in Blobs::new(N).map(|x| sha256(&x)) { println!("{}", checksum); } - // println!("rayon"); - // for checksum in blobs.take(N).par_iter().map(|x| sha256(x)) { - // println!("{}", checksum); - // } + println!("rayon"); + for checksum in Blobs::new(N).par_iter().map(|x| sha256(&x)) { + println!("{}", checksum); + } } struct Blobs { - size: usize, n: usize, } impl Blobs { - fn new(size: usize, n: usize) -> Self { - Self { size, n } + fn new(n: usize) -> Self { + Self { n } + } +} + +struct Blob { + data: [u8; SIZE], +} + +impl Blob { + fn new() -> Self { + Self { data: [0; SIZE] } } } impl Iterator for Blobs { - type Item = Vec; + type Item = Blob; fn next(&mut self) -> Option { if self.n > 0 { self.n -= 1; - Some(vec![0; self.size]) + Some(Blob::new()) } else { None } } } -fn sha256(data: &[u8]) -> String { +fn sha256(blob: &Blob) -> String { let mut hasher = Sha256::new(); - hasher.update(data); + hasher.update(&blob.data); let hash = hasher.finalize(); format!("{:x}", hash) } -- cgit v1.2.1