Skip to content

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

@chan369

Description

@chan369

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions