-
Notifications
You must be signed in to change notification settings - Fork 0
/
rust_threads.rs
41 lines (38 loc) · 944 Bytes
/
rust_threads.rs
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
use std::time::Instant;
use std::{
fs::File,
io::{Read, Write},
thread::JoinHandle,
};
fn compute() {
let handles: Vec<JoinHandle<_>> = (0..1000)
.map(|_| {
std::thread::spawn(move || {
let mut buffer = [0; 10];
{
let mut dev_urandom = File::open("10-bytes").unwrap();
dev_urandom.read(&mut buffer).unwrap();
}
let mut dev_null = File::create("/dev/null").unwrap();
dev_null.write(&mut buffer).unwrap();
})
})
.collect();
for handle in handles {
handle.join().unwrap();
}
}
fn main() {
// warmup
compute();
let before = Instant::now();
for _ in 0..1000 {
compute();
}
let elapsed = before.elapsed();
println!(
"{:?} total, {:?} avg per iteration",
elapsed,
elapsed / 1000
);
}