Debian Codegraph @

.cargo/vendor/once_cell/examples/bench.rs

raw
1 use std::mem::size_of;
2
3 use once_cell::sync::OnceCell;
4
5 const N_THREADS: usize = 32;
6 const N_ROUNDS: usize = 100_000_000;
7
8 static CELL: OnceCell<usize> = OnceCell::new();
9
10 fn main() {
11 let start = std::time::Instant::now();
12 let threads =
13 (0..N_THREADS).map(|i| std::thread::spawn(move || thread_main(i))).collect::<Vec<_>>();
14 for thread in threads {
15 thread.join().unwrap();
16 }
17 println!("{:?}", start.elapsed());
18 println!("size_of::<OnceCell<()>>() = {:?}", size_of::<OnceCell<()>>());
19 println!("size_of::<OnceCell<bool>>() = {:?}", size_of::<OnceCell<bool>>());
20 println!("size_of::<OnceCell<u32>>() = {:?}", size_of::<OnceCell<u32>>());
21 }
22
23 fn thread_main(i: usize) {
24 for _ in 0..N_ROUNDS {
25 let &value = CELL.get_or_init(|| i);
26 assert!(value < N_THREADS)
27 }
28 }