#cpu #cpus #cores

num_cpus

Get the number of CPUs on a machine

28 releases (13 stable)

1.8.0 Dec 29, 2017
1.7.0 Sep 28, 2017
1.6.2 Jun 23, 2017
1.3.0 Feb 28, 2017
0.1.0 Mar 16, 2015

#1 in Hardware support

Download history 33902/week @ 2018-05-27 40436/week @ 2018-06-03 43541/week @ 2018-06-10 38075/week @ 2018-06-17 39927/week @ 2018-06-24 40417/week @ 2018-07-01 39243/week @ 2018-07-08 43022/week @ 2018-07-15 44969/week @ 2018-07-22 39902/week @ 2018-07-29 45761/week @ 2018-08-05 45485/week @ 2018-08-12 40969/week @ 2018-08-19

151,778 downloads per month

num_cpus

crates.io Travis CI Status AppVeyor status

Count the number of CPUs on the current machine.

Usage

Add to Cargo.toml:

[dependencies]
num_cpus = "1.0"

In your main.rs or lib.rs:

extern crate num_cpus;

// count logical cores this process could try to use
let num = num_cpus::get();

lib.rs:

A crate with utilities to determine the number of CPUs available on the current system.

Sometimes the CPU will exaggerate the number of CPUs it contains, because it can use processor tricks to deliver increased performance when there are more threads. This crate provides methods to get both the logical and physical numbers of cores.

This information can be used as a guide to how many tasks can be run in parallel. There are many properties of the system architecture that will affect parallelism, for example memory access speeds (for all the caches and RAM) and the physical architecture of the processor, so the number of CPUs should be used as a rough guide only.

Examples

Fetch the number of logical CPUs.

let cpus = num_cpus::get();

See rayon::Threadpool for an example of where the number of CPUs could be used when setting up parallel jobs (Where the threadpool example uses a fixed number 8, it could use the number of CPUs).

MIT/Apache-2.0 license

Dependencies

Reverse deps