---
title: "让你的CPU利用率画正弦图"
author: "ooaer"
date: "2010-06-24T23:05:30Z"
tags: []
categories: ["编程语言"]
slug: "cpu"
draft: false
lastmod: "2010-06-24T23:05:30Z"
id: 698786
url: "https://ooaer.iteye.com/blog/698786"
---

```C++

#include "windows.h"
#include "stdlib.h"
#include "math.h"

const double SPLIT=0.01;
const int COUNT = 200;
const double PI =3.1415926;
const int INTERVAL = 300;

int main(int argc,char * argv[])
{
DWORD busyspan[count];
DWORD idlespan[count];
int half = INTERVAL/2;
double radian = 0.0;
for(int i=0;i<COUNT;i++)
{
  busyspan[i] = (DWORD)(half+sin(PI*radian)*half);
  idlespan[i] = (DWORD) INTERVAL - busyspan[i];
	//busyspan[i]=idlespan[i]=half;
  radian += SPLIT;
}
DWORD startTime = 0;
int j =0;
while(1)
{
  j = j%COUNT;
 startTime = GetTickCount();//系统每一毫秒累加一次，从系统启动开始计时最多到49.7天
  while((GetTickCount()-startTime)<busyspan[j]);
  Sleep(idlespan[j]);
  j++;
}
```


return 0;
}