Skip to content

Instantly share code, notes, and snippets.

@nukopy
Created June 19, 2023 17:50
Show Gist options
  • Save nukopy/29c3e5b54ab4c7521bf791fa245c85ed to your computer and use it in GitHub Desktop.
Save nukopy/29c3e5b54ab4c7521bf791fa245c85ed to your computer and use it in GitHub Desktop.
Get the number of logical / physical cores in Rust
// ref: https://docs.rs/num_cpus/latest/num_cpus/fn.get.html
fn main() {
let logical_cpus = num_cpus::get();
let physical_cpus = num_cpus::get_physical();
println!("Logical CPUs: {}", logical_cpus);
println!("Physical CPUs: {}", physical_cpus);
/*
if logical_cpus > physical_cpus {
println!(
"We have simultaneous multithreading with about {:.2} \
logical cores to 1 physical core.",
(logical_cpus as f64) / (physical_cpus as f64)
);
} else if logical_cpus == physical_cpus {
println!(
"Either we don't have simultaneous multithreading, or our \
system doesn't support getting the number of physical CPUs."
);
} else {
println!(
"We have less logical CPUs than physical CPUs, maybe we only have access to \
some of the CPUs on our system."
);
}
*/
match logical_cpus.cmp(&physical_cpus) {
std::cmp::Ordering::Greater => {
println!(
"We have simultaneous multithreading with about {:.2} \
logical cores to 1 physical core.",
(logical_cpus as f64) / (physical_cpus as f64)
);
}
std::cmp::Ordering::Equal => {
println!(
"Either we don't have simultaneous multithreading, or our \
system doesn't support getting the number of physical CPUs."
);
}
std::cmp::Ordering::Less => {
println!(
"We have less logical CPUs than physical CPUs, maybe we only have access to \
some of the CPUs on our system."
);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment