summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2021-04-06 11:35:48 +0300
committerLars Wirzenius <liw@liw.fi>2021-04-06 11:35:48 +0300
commit1158c74c9dd36f96f49ef4a7e27b27cac0096b1a (patch)
tree10d79ff93de469e69a0baa46dde6085e03cbdc27
parentf035108b0583ae02bbdf7a8713241c4d88e1e5bb (diff)
downloadchecksum-concurrency-1158c74c9dd36f96f49ef4a7e27b27cac0096b1a.tar.gz
try rayon and fail
-rw-r--r--src/main.rs34
1 files 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<u8>;
+ type Item = Blob;
fn next(&mut self) -> Option<Self::Item> {
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)
}