编程之美-CPU占用率

CPU是4核,3.10GHz,废话不多说,上代码。

代码

#include <stdio.h>
#include <math.h>
#include <windows.h>

/************************************************************************
    1.1 让CPU占用率曲线听你指挥
************************************************************************/


/************************************************************************
    CPU占用50%:解决方法一
************************************************************************/
void first()
{
    while (true)
    {
        for (double i = 0; i < 3 * pow(10.0, 5); i++);

        Sleep(10);
    }
}

/************************************************************************
    CPU占用50%:解决方法二
************************************************************************/
void second()
{
    int busyTime = 10;
    int idleTime = busyTime;
    double startTime = 0;
    while (true)
    {
        startTime = GetTickCount();
        while ((GetTickCount() - startTime) <= busyTime);
        Sleep(idleTime);
    }
}

/************************************************************************
    粗粒度实现CPU占用 percent% 需要微调
************************************************************************/
void third(int percent)
{
    double busyTime = percent;
    double idleTime = 100-percent;
    double startTime = 0;
    while (true)
    {
        startTime = GetTickCount();
        while ((GetTickCount() - startTime) <= busyTime);
        Sleep(idleTime);
    }
}

/************************************************************************
    绘制正弦曲线
************************************************************************/
void four()
{
    // 每次正弦曲线前进0.01*PI,则需要200次
    const double SPLIT = 0.01;
    const int COUNT = 200;
    const double PI = 3.1415926;

    // 峰值300,峰底0
    const int INTERVAL = 300;

    // 忙时和闲时
    double busyTime[COUNT];
    double idleTime[COUNT];

    int half = INTERVAL / 2;
    double radian = 0.0;

    // 获得busyTime和idleTime
    for (int x = 0; x < COUNT; x++)
    {
        busyTime[x] = half + sin(radian*PI) * half;
        idleTime[x] = INTERVAL - busyTime[x];
        radian += SPLIT;
    }

    double startTime = 0;
    int j = 0;
    while (true)
    {
        j = j % COUNT;
        startTime = GetTickCount();
        while (GetTickCount()-startTime <= busyTime[j]);
        Sleep(idleTime[j]);
        j++;
    }
}


int main(int argc, char *argv[])
{

    SYSTEM_INFO info;
    // 调用API函数来获取计算机硬件的信息 
    GetSystemInfo(&info);
    // cpu核数  
    //printf("dwNumberOfProcessors : %d\n",info.dwNumberOfProcessors);

    DWORD dwThreadId;  
    HANDLE hThread = NULL;  
    int coreNum = info.dwNumberOfProcessors;  
    for(int i = 0 ; i < coreNum ; i ++)
    {  
        // 针对third函数 如果是其他三个 hThread = CreateThread(0,0,(LPTHREAD_START_ROUTINE)first,0 ,0,&dwThreadId);
        hThread = CreateThread(0,0,(LPTHREAD_START_ROUTINE)third,(LPVOID)20,0,&dwThreadId);
        // 针对cpu核i(from 0)  
        SetThreadAffinityMask(hThread,1<<i);
    }
    // 传入INFINITE表示无限等待  
    WaitForSingleObject(hThread, INFINITE); 

    return 0;
}

效果图

50%CPU占用

这里写图片描述

粗粒度任意CPU占用率

这里写图片描述

正弦曲线

这里写图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值