Windows系统CPU内存网络性能统计第四篇 CPU 多核CPU各核使用率C++

本文介绍了如何在VC++中引用C#代码来统计Windows系统多核CPU各核的使用率。通过创建一个C#类库,提供获取CPU使用率的函数,然后在C++中调用该DLL实现统计功能。程序运行结果显示了各核的使用情况。作者计划寻找在C++中直接获取CPU使用率的方法,并欢迎读者交流。
               

转载请标明出处,原文地址:http://blog.youkuaiyun.com/morewindows/article/details/8678396

欢迎关注微博:http://weibo.com/MoreWindows

 

Windows系统CPU内存网络性能统计第四篇 CPU多核CPU各核使用率 C++

http://blog.youkuaiyun.com/morewindows/article/details/8678396

 

    本篇《Windows系统CPU内存网络性能统计第四篇 CPU多核CPU各核使用率 C++》(http://blog.youkuaiyun.com/morewindows/article/details/8678396)将介绍在VC++中引用C#代码来完成对多核CPU各核使用率的统计。

Windows系统CPU内存网络性能统计博客目录:

1.Windows系统CPU内存网络性能统计第一篇内存

http://blog.youkuaiyun.com/morewindows/article/details/8459219

2.Windows系统CPU内存网络性能统计第二篇 CPU CPU整体使用率

http://blog.youkuaiyun.com/morewindows/article/details/8678359

3.Windows系统CPU内存网络性能统计第三篇 CPU 多核CPU各核使用率 C#

http://blog.youkuaiyun.com/morewindows/article/details/8678382

4.Windows系统CPU内存网络性能统计第四篇 CPU多核CPU各核使用率 C++

http://blog.youkuaiyun.com/morewindows/article/details/8678396

 

首先是C#代码。注意这是一个“C#类库”的工程,在此工程中完成了一个CShapeCPUUseRate类,这个类的GetCPUEveryCoreUseRate函数将返回一个包含各CPU各核使用率的字符串,比如双核CPU一个核的使用率是3%,另一个的使用率是5%,那么将返回"3,5"。

//Windows系统CPU内存网络性能统计第四篇 CPU多核CPU各核使用率 C++//http://blog.youkuaiyun.com/morewindows/article/details/8678396using 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.youkuaiyun.com/morewindows/article/details/8678431)。

//Windows系统CPU内存网络性能统计第四篇CPU多核CPU各核使用率C++//http://blog.youkuaiyun.com/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.youkuaiyun.com/morewindows/article/details/8678396 -\n"); printf(" -- By MoreWindows( http://blog.youkuaiyun.com/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.youkuaiyun.com/morewindows/article/details/8678396

欢迎关注微博:http://weibo.com/MoreWindows

 



 

           

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.youkuaiyun.com/jiangjunshow

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值