Skip to content

Commit e5436a5

Browse files
fix(divan): use busy sleep in time scale examples
For these times, using `thread::sleep` makes no sense as, at least on linux, the scheduler's timer resolution is orders of magnitude larger than nanoseconds.
1 parent 550d10f commit e5436a5

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

crates/divan_compat/examples/benches/time_scale.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,29 @@ fn main() {
22
divan::main();
33
}
44

5+
fn busy_sleep(duration: std::time::Duration) {
6+
let start = std::time::Instant::now();
7+
while start.elapsed() < duration {}
8+
}
9+
510
#[divan::bench]
611
fn sleep_1ns() {
7-
std::thread::sleep(std::time::Duration::from_nanos(1));
12+
busy_sleep(std::time::Duration::from_nanos(1));
813
}
914

1015
#[divan::bench]
1116
fn sleep_100ns() {
12-
std::thread::sleep(std::time::Duration::from_nanos(100));
17+
busy_sleep(std::time::Duration::from_nanos(100));
1318
}
1419

1520
#[divan::bench]
1621
fn sleep_1us() {
17-
std::thread::sleep(std::time::Duration::from_micros(1));
22+
busy_sleep(std::time::Duration::from_micros(1));
1823
}
1924

2025
#[divan::bench]
2126
fn sleep_100us() {
22-
std::thread::sleep(std::time::Duration::from_micros(100));
27+
busy_sleep(std::time::Duration::from_micros(100));
2328
}
2429

2530
#[divan::bench]

0 commit comments

Comments
 (0)