c++怎么获取指定cpu使用率
这个不是c++的语法的问题,要查相应平台的API,window就用window的API,unix就查unix的API。
查到API后,直接调用可能再需要计算一下就可以了得到cpu占有率的api函数:
getsystemtimes
得到内存使用情况的api函数:
globalmemorystatusex function
retrieves information about the system’s current usage of both physical and virtual memory。
getperformanceinfo function
retrieves the performance values contained in the performance_information structure
获取特定程序的内存使用情况用:
getprocessmemoryinfo function
retrieves information about the memory usage of the specified process。
我写的一个cpu使用率例子:
#define _win32_winnt0x0501
#include
#include
usingnamespacestd;
__int64 comparefiletime ( filetime time1, filetime time2 )
__int64 a = time1。dwhighdatetime << 32 | time1。dwlowdatetime ;
__int64 b = time2。dwhighdatetime << 32 | time2。dwlowdatetime ;
return(b – a);
void main()
handle hevent;
bool res ;
filetime preidletime;
filetime prekerneltime;
filetime preusertime;
filetime idletime;
filetime kerneltime;
filetime usertime;
res = getsystemtimes( &idletime, &kerneltime, &usertime );
preidletime = idletime;
prekerneltime = kerneltime;
preusertime = usertime ;
hevent = createevent (null,false,false,null); // 初始值为 nonsignaled ,并且每次触发后自动设置为nonsignaled
while (1){
waitforsingleobject( hevent,1000 ); //等待500毫秒
res = getsystemtimes( &idletime, &kerneltime, &usertime );
int idle = comparefiletime( preidletime,idletime);
int kernel = comparefiletime( prekerneltime, kerneltime);
int user = comparefiletime(preusertime, usertime);
int cpu = (kernel +user – idle) *100/(kernel+user);
int cpuidle = ( idle) *100/(kernel+user);
cout << “cpu利用率:” << cpu << “%” << “cpu空闲率:” <
2016-08-23
0
1、这个不是c++的语法的问题,要查相应平台的API,window就用window的API,unix就查unix的API。
2、查到API后,直接调用可能再需要计算一下就可以了
win7中如何指定单CPU
不知道我的答案是不是你想要的 你可以先打开运行的程序 然后打开任务管理器 再到进程 里面找到这个程序后单击右键选择【设置相关性】 里面选择cpu这个win7不能设定的!只有电脑程序需要全开,或者专用的软件才能让cpu使用率100%