mirror of
https://github.com/KaiserY/trpl-zh-cn
synced 2025-05-24 02:28:07 +08:00
37 lines
880 B
Rust
37 lines
880 B
Rust
extern crate trpl; // required for mdbook test
|
|
|
|
use std::time::{Duration, Instant};
|
|
|
|
fn main() {
|
|
trpl::run(async {
|
|
// ANCHOR: here
|
|
let one_ns = Duration::from_nanos(1);
|
|
let start = Instant::now();
|
|
async {
|
|
for _ in 1..1000 {
|
|
trpl::sleep(one_ns).await;
|
|
}
|
|
}
|
|
.await;
|
|
let time = Instant::now() - start;
|
|
println!(
|
|
"'sleep' version finished after {} seconds.",
|
|
time.as_secs_f32()
|
|
);
|
|
|
|
let start = Instant::now();
|
|
async {
|
|
for _ in 1..1000 {
|
|
trpl::yield_now().await;
|
|
}
|
|
}
|
|
.await;
|
|
let time = Instant::now() - start;
|
|
println!(
|
|
"'yield' version finished after {} seconds.",
|
|
time.as_secs_f32()
|
|
);
|
|
// ANCHOR_END: here
|
|
});
|
|
}
|