Skip to content

Commit ffec6ee

Browse files
chore(divan_compat_examples): switch time_scale benches to busy sleeping
Since the os thread scheduler is orders of magnitude ahead, it makes no sense to use thread::sleep for such small benchmarks
1 parent cd70239 commit ffec6ee

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

crates/divan_compat/examples/benches/time_scale.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,32 @@ fn main() {
22
divan::main();
33
}
44

5+
fn busy_sleeping(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_sleeping(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_sleeping(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_sleeping(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_sleeping(std::time::Duration::from_micros(100));
2328
}
2429

2530
#[divan::bench]
2631
fn sleep_1ms() {
27-
std::thread::sleep(std::time::Duration::from_millis(1));
32+
busy_sleeping(std::time::Duration::from_millis(1));
2833
}

0 commit comments

Comments
 (0)