加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

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

发布时间:2020-12-14 02:29:41 所属栏目:Windows 来源:网络整理
导读:分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!http://www.captainbed.net 转载请标明出处,原文地址:http://www.voidcn.com/article/p-kzaidrpe-yt.html 欢迎关注微博: http://we

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

转载请标明出处,原文地址:http://www.voidcn.com/article/p-kzaidrpe-yt.html

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

?

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

http://www.voidcn.com/article/p-kzaidrpe-yt.html

?

??? 本篇《Windows系统CPU内存网络性能统计第四篇 CPU多核CPU各核使用率 C++(http://www.voidcn.com/article/p-kzaidrpe-yt.html)将介绍在VC++中引用C#代码来完成对多核CPU各核使用率的统计。

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

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

http://www.voidcn.com/article/p-yhagajdp-yt.html

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

http://www.voidcn.com/article/p-zgctylfj-yt.html

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

http://www.voidcn.com/article/p-ekqbwdaf-yt.html

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

http://www.voidcn.com/article/p-kzaidrpe-yt.html

?

首先是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://www.voidcn.com/article/p-eesrcdum-yt.html)

//Windows系统CPU内存网络性能统计第四篇CPU多核CPU各核使用率C++
//http://blog.csdn.net/morewindows/article/details/8678396
//#using "CShapeCPUUseRateDLLCShapeCPUUseRateDLLbinDebugCShapeCPUUseRateDLL.dll"
#using "CShapeCPUUseRateDLLCShapeCPUUseRateDLLbinReleaseCShapeCPUUseRateDLL.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 ) --nn"); 

	CShapeCPUUseRate ^ cpuUseRate = gcnew CShapeCPUUseRate;
	if (!cpuUseRate->Initialize())
	{
		printf("Error!n");
		getch();
		return -1;
	}
	else
	{
		printf("系统中CPU为%d核CPUn",cpuUseRate->GetCPUCoreNumber());
		while (true)
		{	
			Sleep(1000);
			printf("r当前CPU各核使用率分别为:%s ",cpuUseRate->GetCPUEveryCoreUseRate());
		}
	}
	return 0;
}

程序运行结果如下:

?

这种通过C++调用C#代码来获取CPU各核使用率的方法不是太好,以后再找找资料看看在C++中如何直接获取CPU各核使用率,欢迎高手指点。

转载请标明出处,原文地址:http://www.voidcn.com/article/p-kzaidrpe-yt.html

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

?



?

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读