转载请标明出处,原文地址:http://blog.csdn.net/morewindows/article/details/8678396
欢迎关注微博:http://weibo.com/MoreWindows
Windows系统CPU内存网络性能统计第四篇 CPU多核CPU各核使用率 C++
http://blog.csdn.net/morewindows/article/details/8678396
本篇《Windows系统CPU内存网络性能统计第四篇 CPU多核CPU各核使用率 C++》(http://blog.csdn.net/morewindows/article/details/8678396)将介绍在VC++中引用C#代码来完成对多核CPU各核使用率的统计。
Windows系统CPU内存网络性能统计博客目录:
1.Windows系统CPU内存网络性能统计第一篇内存
http://blog.csdn.net/morewindows/article/details/8459219
2.Windows系统CPU内存网络性能统计第二篇 CPU CPU整体使用率
http://blog.csdn.net/morewindows/article/details/8678359
3.Windows系统CPU内存网络性能统计第三篇 CPU 多核CPU各核使用率 C#
http://blog.csdn.net/morewindows/article/details/8678382
4.Windows系统CPU内存网络性能统计第四篇 CPU多核CPU各核使用率 C++
http://blog.csdn.net/morewindows/article/details/8678396
首先是C#代码。注意这是一个“C#类库”的工程,在此工程中完成了一个CShapeCPUUseRate类,这个类的GetCPUEveryCoreUseRate函数将返回一个包含各CPU各核使用率的字符串,比如双核CPU一个核的使用率是3%,另一个的使用率是5%,那么将返回"3,5"。
//Windows系统CPU内存网络性能统计第四篇 CPU多核CPU各核使用率 C++
//http://blog.csdn.net/morewindows/article/details/8678396
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace CShapeCPUUseRateDLL
{
public class CShapeCPUUseRate
{
public int Initialize()
{
try
{
m_nCPUCoreNumber = System.Environment.ProcessorCount;
m_pfCounters = new PerformanceCounter[m_nCPUCoreNumber];
for(int i = 0; i < m_nCPUCoreNumber; i++)
{
m_pfCounters[i] = new PerformanceCounter("Processor", "% Processor Time", i.ToString());
}
}
catch (System.Exception e)
{
return 0;
}
return 1;
}
public int GetCPUCoreNumber()
{
return m_nCPUCoreNumber;
}
public string GetCPUEveryCoreUseRate()
{
StringBuilder strBuild = new StringBuilder();
float fRate = m_pfCounters[0].NextValue();
int nRate = Convert.ToInt32(fRate);
strBuild.Append(nRate.ToString());
for(int i = 1; i < m_nCPUCoreNumber; i++)
{
fRate = m_pfCounters[i].NextValue();
nRate = Convert.ToInt32(fRate);
strBuild.Append("," + nRate.ToString());
}
return strBuild.ToString();
}
private PerformanceCounter[] m_pfCounters;
private int m_nCPUCoreNumber;
}
}
如何在C++调用C#代码可以参考《C++通过DLL调用C#代码》(http://blog.csdn.net/morewindows/article/details/8678431)。
//Windows系统CPU内存网络性能统计第四篇CPU多核CPU各核使用率C++
//http://blog.csdn.net/morewindows/article/details/8678396
//#using "CShapeCPUUseRateDLL\\CShapeCPUUseRateDLL\\bin\\Debug\\CShapeCPUUseRateDLL.dll"
#using "CShapeCPUUseRateDLL\\CShapeCPUUseRateDLL\\bin\\Release\\CShapeCPUUseRateDLL.dll"
#include <Windows.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
using namespace CShapeCPUUseRateDLL;
int main()
{
printf(" Windows系统CPU内存网络性能统计第四篇CPU多核CPU各核使用率C++\n");
printf(" - http://blog.csdn.net/morewindows/article/details/8678396 -\n");
printf(" -- By MoreWindows( http://blog.csdn.net/MoreWindows ) --\n\n");
CShapeCPUUseRate ^ cpuUseRate = gcnew CShapeCPUUseRate;
if (!cpuUseRate->Initialize())
{
printf("Error!\n");
getch();
return -1;
}
else
{
printf("系统中CPU为%d核CPU\n",cpuUseRate->GetCPUCoreNumber());
while (true)
{
Sleep(1000);
printf("\r当前CPU各核使用率分别为:%s ", cpuUseRate->GetCPUEveryCoreUseRate());
}
}
return 0;
}
程序运行结果如下:
这种通过C++调用C#代码来获取CPU各核使用率的方法不是太好,以后再找找资料看看在C++中如何直接获取CPU各核使用率,欢迎高手指点。
转载请标明出处,原文地址:http://blog.csdn.net/morewindows/article/details/8678396
欢迎关注微博:http://weibo.com/MoreWindows
分享到:
相关推荐
《Windows系统CPU内存网络性能统计第三篇 CPU 多核CPU各核使用率 C#》 http://blog.csdn.net/morewindows/article/details/8678382 配套程序,能获取多核CPU各核的使用率,已经测试,能在WinXP及Win7运行。
visual c++让多核CPU占用率达到100%
获得WIN7多核情况下,CPU各个核使用率的值
应用:客户将所有的刷新动作交给主线程完成时,可能拖动鼠标,窗口均在不断的刷新,CPU资源占用率很高,导致整个程序运行速度变慢,这样时候,可以考虑将一些刷新显示的工作绑定到另一颗核中去实现。 linux ...
cpu 使用率 cpu 使用率 cpu 使用率 cpu 使用率 cpu 使用率
C++多核编程及其代码,2010年发行,是多核编程的经典书籍,需要的人就不用我多说了
CPU双核 多核补丁自动版CPU双核_多核补丁自动版
c++多核高级编程中文高清扫描版,带标签,强烈推荐
该应用脚本是为了提高机器的CPU使用率,它会根据当前机器CPU (会自动排除本程序进程所占用CPU%) 决定是否需要”协助“已提高当前机器的CPU的使用率;
获取多核CPU使用率,设置CPU亲和性,设置并发线程数和优先级,绑定线程在特点CPU上运行,并测试
Windows操作系统下的多核CPU内核线程的管理方法。
OpenMP详细用法,OMP太牛B了能够发辉多核CPU100的性能
写一个小程序在后台记录每个进程的CPU使用情况,揪出锁屏后占用CPU的进程,于是自己写了一个C++类CPUusage,方便地监视不同进程的CPU占用情况。本人编程还只是个新手,如有问题请多多指教
xp3下发挥双核 的性能.因为特别是双核 在winxp下性能不是很完善,所以我用之后好一些,推荐
解决问题:在电脑中使用旧版服务器系统无法使用现在的多核CPU (即在任务管理器中显示为单核) 全面支持 Windows XP Sp2、Windows XP Sp3、Windows Server 2003 SP2 系统
利用多cpu或多核cpu求解deform3d案例的方法
这是一个很好的windows多核CPU管理的一个文档
vs2008环境,vc编写的一个小工具,获取指定进程名字的cpu占用率和内存占用量,并按日期记录到ini配置文件中,支持多核cpu,如果有类似需求的,就不用再写代码了,下载下去看看吧。
针对多核CPU和GPU环境下图的深度优先搜索问题,提出多核CPU中实现并行DFS的新算法,通过有效利用内存带宽来提高性能,且当图增大时优势越明显。在此基础上提出一种混合方法,为DFS每一分支动态地选择最佳的实现:...