Precise timing about Instant

use std::time::{Instant, Duration};
use std::thread;

fn main() {
    let instant = Instant::now();

    println!("{}", instant.elapsed().as_nanos());

# cargo run --release 
C:\Users\Administrator\CLionProjects\testy>cargo run --release
    Finished release [optimized] target(s) in 0.00s
     Running `target\release\testy.exe`

how can i precise timing? the result is not what I want.

That is the precise timing. thread::sleep isn't guaranteed to be precise so that's where the fluctuations come from

1 Like

It's up to the OS scheduler how long the sleep actually lasts:

The thread may sleep longer than the duration specified due to scheduling specifics or platform-dependent functionality. It will never sleep less.

You can increase the timer resolution via some Windows API calls (not sure how you'd do the equivalent for other platforms), but sleeping will never be 100% precise.


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.