You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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?
The text was updated successfully, but these errors were encountered:
My former comment assumed Unix. For Windows, I'm not sure what's going on -- I don't think there are any locks in chrono's Windows code so I'm guessing something in the OS API is causing contention?
I've not looked into it yet but my immediate reaction in such cases is to always check allocations first. The built-in Windows allocator is synchronous so a lot of threads allocating at the same time is going to be slow.
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?
The text was updated successfully, but these errors were encountered: