num_cpus

Get the number of CPUs on a machine

38 releases (23 stable)

Uses old Rust 2015

1.16.0 Jun 29, 2023
1.15.0 Dec 20, 2022
1.14.0 Nov 3, 2022
1.13.1 Dec 20, 2021
0.1.0 Mar 16, 2015

#1 in Hardware support

Download history 1948011/week @ 2024-06-26 1778159/week @ 2024-07-03 1946304/week @ 2024-07-10 2000564/week @ 2024-07-17 1744920/week @ 2024-07-24 1675467/week @ 2024-07-31 1716101/week @ 2024-08-07 1589391/week @ 2024-08-14 1591367/week @ 2024-08-21 1505229/week @ 2024-08-28 1667417/week @ 2024-09-04 1600407/week @ 2024-09-11 1695300/week @ 2024-09-18 1832411/week @ 2024-09-25 2183550/week @ 2024-10-02 1922251/week @ 2024-10-09

7,917,912 downloads per month
Used in 12,463 crates (1,523 directly)

MIT/Apache

34KB
792 lines

num_cpus

crates.io CI 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).

Dependencies

~53KB