編程之美-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佔用率

這裏寫圖片描述

正弦曲線

這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章