Microsoft---控制CPU的曲线

#include <iostream>
#include<stdlib.h>
#include<Windows.h>
using namespace std;
int main()
{
	int s=2;
	for(;;)
	{
		for (int i = 0; i < 9600000; i++)
			;
		Sleep(10);

	}
		
	
	return 0;
}
/*#include "Windows.h"
#include "stdlib.h"
#include "math.h"

 const double SPLIT = 0.01;
const int COUNT = 200;
 const double PI = 3.14159265;
const int INTERVAL = 300;

int main()
{
	DWORD busySpan[COUNT];  //array of busy times
	DWORD idleSpan[COUNT];  //array of idle times
	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] = INTERVAL - busySpan[i];
		radian += SPLIT;
	}
	DWORD startTime = 0;
	int j = 0;
	while (true)
	{
		j = j % COUNT;
		startTime = GetTickCount();
		while ((GetTickCount() - startTime) <= busySpan[j]);
		Sleep(idleSpan[j]);
		j++;
	}
	return 0;
}*/

//C++ code to make task manager generate sine graph
//#include<cstdlib>
/*#include<cmath>
#include<windows.h>

void drawSine()
{
	const double PI = 3.1415926536; //pi值
	const int SAMPLING_COUNT = 200; //一个周期内采样点数量
									//INTERVAL对应原程序的TOTAL_AMPLITUDE
	const int INTERVAL = 300; //两次采样的时间间隔

							  //由于正弦曲线是周期性变化,只要处理一个周期就够了。
							  //把曲线的一个周期划分成SAMPLING_COUNT份进行采样,
							  //两次采样的时间间隔为INTERVAL毫秒,
							  //计算每个采样点的振幅值,该值就是在这段INTERVAL时间内CPU工作时间所占的比率

							  // 个人倾向于使用两个常量周期PERIOD和采样数SAMPLING_COUNT,
							  // PERIOD值决定了形状,可以通过改变SAMPLING_COUNT值调整所画曲线的精确度
							  // const double PERIOD = 60000; //60000 ms 一个周期
							  // const int SAMPLING_COUNT = 200; //200个点
							  // const double INTERVAL = PERIOD / SAMPLING_COUNT; //采样间隔

	const double radianIncrement = 2.0 * PI / SAMPLING_COUNT; //抽样弧度的增量

	DWORD busyTime[SAMPLING_COUNT]; //busyTime 对应原程序的 busySpan
	double radian = 0.0;
	for (int i = 0; i < SAMPLING_COUNT; i++) {
		double busyRatio = 0.5 * (1 + sin(radian));
		busyTime[i] = (DWORD)(busyRatio * INTERVAL);
		radian += radianIncrement;
	}

	DWORD startTime = 0;
	for (int j = 0; ; j = (j + 1) % SAMPLING_COUNT) {
		startTime = GetTickCount();
		while ((GetTickCount() - startTime) <= busyTime[j]) {}
		//Sleep(INTERVAL - busyTime[j]);
		DWORD idleTime = INTERVAL - busyTime[j];
		Sleep(idleTime);
	}
}


int main()
{
	drawSine();
	return 0;
}
这篇文章都是前人的劳动成果,我只是记录下这个坑,因为没明白。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

佳悦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值