• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use lazy_static::lazy_static;
2 use once_cell::sync::Lazy;
3 
4 const N_THREADS: usize = 32;
5 const N_ROUNDS: usize = 100_000_000;
6 
7 static ONCE_CELL: Lazy<Vec<String>> = Lazy::new(|| vec!["Spica".to_string(), "Hoyten".to_string()]);
8 
9 lazy_static! {
10     static ref LAZY_STATIC: Vec<String> = vec!["Spica".to_string(), "Hoyten".to_string()];
11 }
12 
main()13 fn main() {
14     let once_cell = {
15         let start = std::time::Instant::now();
16         let threads = (0..N_THREADS)
17             .map(|_| std::thread::spawn(move || thread_once_cell()))
18             .collect::<Vec<_>>();
19         for thread in threads {
20             thread.join().unwrap();
21         }
22         start.elapsed()
23     };
24     let lazy_static = {
25         let start = std::time::Instant::now();
26         let threads = (0..N_THREADS)
27             .map(|_| std::thread::spawn(move || thread_lazy_static()))
28             .collect::<Vec<_>>();
29         for thread in threads {
30             thread.join().unwrap();
31         }
32         start.elapsed()
33     };
34 
35     println!("once_cell:   {:?}", once_cell);
36     println!("lazy_static: {:?}", lazy_static);
37 }
38 
thread_once_cell()39 fn thread_once_cell() {
40     for _ in 0..N_ROUNDS {
41         let len = ONCE_CELL.len();
42         assert_eq!(len, 2)
43     }
44 }
45 
thread_lazy_static()46 fn thread_lazy_static() {
47     for _ in 0..N_ROUNDS {
48         let len = LAZY_STATIC.len();
49         assert_eq!(len, 2)
50     }
51 }
52