关于编程控制CPU使用率的笔记
2008-09-20 15:26
204 查看
代码
#include "Windows.h"
#include "stdlib.h"
#include "math.h"
#include <tchar.h>
const double SPLIT = 0.01;
const int COUNT = 200;
const double PI = 3.14159265;
const int INTERVAL = 300;
int _tmain(int argc, _TCHAR* argv[])
#include "stdafx.h"
#include "Windows.h"
#include "stdlib.h"
#include "math.h"
#include <tchar.h>
const double SPLIT = 0.01;
const int COUNT = 200;
const double PI = 3.14159265;
const int INTERVAL = 300;
int _tmain(int argc, _TCHAR* argv[])
{
SetProcessAffinityMask(
GetCurrentProcess(),
0x00000001 //cpu mask
);
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;
}
#include "Windows.h"
#include "stdlib.h"
#include "math.h"
#include <tchar.h>
const double SPLIT = 0.01;
const int COUNT = 200;
const double PI = 3.14159265;
const int INTERVAL = 300;
int _tmain(int argc, _TCHAR* argv[])
#include "stdafx.h"
#include "Windows.h"
#include "stdlib.h"
#include "math.h"
#include <tchar.h>
const double SPLIT = 0.01;
const int COUNT = 200;
const double PI = 3.14159265;
const int INTERVAL = 300;
int _tmain(int argc, _TCHAR* argv[])
{
SetProcessAffinityMask(
GetCurrentProcess(),
0x00000001 //cpu mask
);
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;
}
相关文章推荐
- 关于网络编程(服务端)的一些笔记
- think in java第十五章关于网络编程的读书笔记
- 编程笔记(objective-c :关于属性)
- 关于Linux下mysql的C/C++基础编程笔记
- 关于QHttp编程的一些笔记(转)
- 关于网络编程(服务端)的一些笔记
- UNIX环境编程学习笔记(27)——多线程编程(二):控制线程属性
- 编程笔记(objective-c :关于属性)
- 关于多线程编程的笔记
- 关于网络编程(服务端)的一些笔记 - roen的专栏 - 博客频道 - CSDN.NET
- 【Java编程思想笔记】第六章-访问权限控制
- Java编程思想笔记02:组合与继承、final、策略设计模式与适配器模式、内部类、序列化控制(注意事项)
- Go学习笔记 - 关于Java、Python、Go编程思想的不同
- 【java编程思想--学习笔记(二)】访问控制-Java访问权限修饰词
- 《Java 编程思想》-第6章 访问权限控制 笔记
- Go学习笔记:关于Java、Python、Go编程思想的不同
- UNIX环境编程学习笔记(27)——多线程编程(二):控制线程属性
- UNIX环境高级编程笔记之进程控制
- 关于网络编程(服务端)的一些笔记
- 《Java 编程思想》-第4章 控制执行流程 笔记