Skip to content

Instantly share code, notes, and snippets.

@peterjoel
Created July 7, 2019 14:12
Show Gist options
  • Save peterjoel/f615e78170cf44c3d67d43acbb1c860d to your computer and use it in GitHub Desktop.
Save peterjoel/f615e78170cf44c3d67d43acbb1c860d to your computer and use it in GitHub Desktop.
[package]
name = "lukas_threads"
version = "0.1.0"
authors = ["Peter Hall <peterjoel@gmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
audio_thread_priority = "0.15.0"
use std::time::{Duration, Instant};
use audio_thread_priority::{RtPriorityHandle, promote_current_thread_to_real_time};
const ITERS: usize = 100;
fn main() {
match promote_current_thread_to_real_time(100, 200_000) {
Ok(_handle) => {
// A dummy variable to prevent the compiler from removing the dummy prime
// code.
let mut x = 0;
// Iterate over different target loop times
for loop_time in (1..30).map(|n| Duration::from_millis(n)) {
let mut payload_duration = Duration::from_millis(0);
for _ in 0..ITERS {
let before = Instant::now();
x += count_primes(3_500);
let elapsed = before.elapsed();
payload_duration += elapsed;
// Sleep the remaining time
if loop_time > elapsed {
std::thread::sleep(loop_time - elapsed);
}
}
let avg_duration = payload_duration / ITERS as u32;
println!("loop_time {:.2?} \t=> {:.2?}", loop_time, avg_duration);
}
println!("{}", x);
}
Err(err) => {
eprintln!("Error couldn't promote thread: {:?}", err);
}
}
}
/// Dummy function.
fn count_primes(up_to: u64) -> u64 {
(2..up_to)
.filter(|n| (2..n / 2).all(|d| n % d != 0))
.count() as u64
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment