blob: 893d27d0cd9d608e602b3d983edbf281c5840f19 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
use rayon::prelude::*;
use sha2::{Digest, Sha256};
const N: usize = 10;
const SIZE: usize = 128 * 1024;
fn main() {
println!("sequential");
for checksum in Blobs::new(N).map(|x| sha256(&x)) {
println!("{}", checksum);
}
println!("rayon");
Blobs::new(N)
.par_bridge()
.map(|x| sha256(&x))
.for_each(|x| println!("{}", x));
}
struct Blobs {
n: usize,
}
impl Blobs {
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 = Blob;
fn next(&mut self) -> Option<Self::Item> {
if self.n > 0 {
self.n -= 1;
Some(Blob::new())
} else {
None
}
}
}
fn sha256(blob: &Blob) -> String {
let mut hasher = Sha256::new();
hasher.update(&blob.data);
let hash = hasher.finalize();
format!("{:x}", hash)
}
|