如何获取真实核心数?

ujh*_*ujh 5 rust

num_cpus箱提供了核心数量,但这包括超线程核心。就我而言,这比使用真正的核心慢得多(几乎是 2 倍)。

Kon*_*hov 1

您可以通过使用hwloc库的rust绑定来完成此操作,如本博客文章中所述。

它可以在各种平台下使用,并允许您获取物理核心和逻辑处理单元的数量(在超线程的情况下),如下所示(代码来自博客文章):

extern crate hwloc;

use hwloc::{Topology, ObjectType};

fn main() {
    // Create a new Topology
    let topology = Topology::new();

    // Get all objects with type "Core"
    let cores = topology.objects_with_type(&ObjectType::Core);

    // Match on the returned Result and print the length if successful.
    match cores {
       Ok(c) => println!("There are {} cores on this machine.", c.len()),
       Err(e) => panic!(format!("Could not load cores because of: {:?}", e))
    }
}
Run Code Online (Sandbox Code Playgroud)