chrono icon indicating copy to clipboard operation
chrono copied to clipboard

Multithreaded calls to Local::now() take a lot of time, which is abnormal.

Open chan369 opened this issue 11 months ago • 4 comments

When Local::now() is called 10,000 times in a single thread, it barely takes any time. However, when called in a multithreaded environment, it takes significantly more time. Below is the example code: fn test_proc() { let i=Instant::now(); for _ in 0..10000 { let v=Local::now(); } let d=i.elapsed(); println!("time:{}",d.as_millis()); }

fn main() { println!("first time"); for _ in 0..1 { thread::spawn(test_proc); } sleep(Duration::from_secs(5)); println!("second time"); for _ in 0..10 { thread::spawn(test_proc); } sleep(Duration::from_secs(5)); }

Output: first time time:4 second time time:362 time:363 time:363 time:365 time:364 time:364 time:365 time:365 time:364 time:365

What's happening here?

chan369 avatar Dec 04 '24 05:12 chan369