如何以编程方式获得CPU缓存页大小在C + +? [英] How to programmatically get the CPU cache page size in C++?

查看:529
本文介绍了如何以编程方式获得CPU缓存页大小在C + +?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的程序读取它在C ++中运行的CPU的缓存行大小。

I'd like my program to read the cache line size of the CPU it's running on in C++.

我知道这不能移植,所以我需要一个解决方案为Linux和另一个为Windows(其他系统的解决方案可能对其他人有用,所以如果你知道他们发布他们)。

I know that this can't be done portably, so I will need a solution for Linux and another for Windows (Solutions for other systems could be usefull to others, so post them if you know them).

对于Linux我可以读取/ proc / cpuinfo的内容,并解析用cache_alignment开头的行。也许有一个更好的方法涉及调用API。

For Linux I could read the content of /proc/cpuinfo and parse the line begining with cache_alignment. Maybe there is a better way involving a call to an API.

对于Windows我根本不知道。

For Windows I simply have no idea.

推荐答案

在Windows上

#include <Windows.h>
#include <iostream>

using std::cout; using std::endl;

int main()
{
    SYSTEM_INFO systemInfo;
    GetSystemInfo(&systemInfo);
    cout << "Page Size Is: " << systemInfo.dwPageSize;
    getchar();
}

在Linux上

http://linux.die.net/man/2/getpagesize

这篇关于如何以编程方式获得CPU缓存页大小在C + +?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆