forked from alexheretic/spin-sleep
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A crude benchmark to compare spin loop hint to yield
- Loading branch information
1 parent
a68f8a8
commit c59197c
Showing
3 changed files
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[package] | ||
name = "latency" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
num_cpus = "1.13.1" | ||
rand = "0.8.4" | ||
spin_sleep = { path = "../.." } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use std::time::{Duration, Instant}; | ||
|
||
use rand::Rng; | ||
|
||
fn main() { | ||
let interval = Duration::from_millis(15); | ||
|
||
let cpus = num_cpus::get(); | ||
let spinners: Vec<_> = (0..cpus).map(|_| { | ||
std::thread::spawn(|| { | ||
let mut rng = rand::thread_rng(); | ||
// Run a lottery instead of boring infinite loop so that it doesn't get misoptimized | ||
while rng.gen::<u64>() > 0 {} | ||
}) | ||
}).collect(); | ||
|
||
let mut i = 0; | ||
let mut sum = Duration::default(); | ||
loop { | ||
let now = Instant::now(); | ||
let deadline = now + interval; | ||
spin_sleep::sleep(interval); | ||
sum += Instant::now() - deadline; | ||
i += 1; | ||
if i % 100 == 0 { | ||
dbg!(sum / 100); | ||
sum = Duration::default(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters