bench.rs 831 B

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