rust - 我怎样才能得到真正的核心数?

标签 rust

num_cpus crate 给了我内核的数量,但这包括超线程内核。在我的例子中,这比使用真正的核心慢得多(几乎是 2 倍)。

最佳答案

您可以使用 rust binding 来做到这一点对于 hwloc此博客中描述的库 post .

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

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))
    }
}

关于rust - 我怎样才能得到真正的核心数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34205771/

相关文章:

rust - 如何从 Arc<Mutex<T>> 获取 T 的所有权?

postgresql - 在Rust中将时间戳吸收到postgres时,如何避免字符串转换?

rust - 是否可以在编译时填充一个大集合?

generics - Rust 宏接受具有泛型参数的类型

rust - 获取切片中最大或最小浮点值的索引或 Rust 中的 Vec 的惯用方法是什么?

rust - 如何引用不安全 block 中使用的变量?

memory - 在 Rust 中的 mem::replace

rust - 加载.env并使用辅助函数将其转换为一般结构

rust - 从单个元素创建迭代器

rust - 未使用 termion 恢复终端状态