让CPU占用率曲线听你指挥
- 章节名:让CPU占用率曲线听你指挥
//C++ code to make task manager generate sine graph #include "windows.h" #include "stdlib.h" #include "math.h" //把一条正弦曲线0~2π之间的弧度等分成200份进行抽样,计算每个抽样点的振幅 //然后每隔300ms的时间取下一个抽样点,并让CPU工作对应振幅的时间 const int SAMPLING_COUNT = 200; //抽样点数量 const double PI = 3.1415926535; //pi值 const int TOTAL_AMPLITUDE = 300; //每个抽样点对应的时间片 int _tmain(int argc, _TCHAR* argv[]) { DWORD busySpan[SAMPLING_COUNT]; int amplitude = TOTAL_AMPLITUDE / 2; double radian = 0.0; double radianIncrement = 2.0 / (double)SAMPLING_COUNT; //抽样弧度的增量 for (int i = 0; i < SAMPLING_COUNT; i++) { busySpan[i] = (DWORD)(amplitude + (sin(PI * radian) * amplitude)); radian += radianIncrement; // printf("%d\t%d\n", busySpan[i], TOTAL_AMPLITUDE - busySpan[i]); } DWORD startTime = 0; for (int j = 0;;j = (j + 1) % SAMPLING_COUNT) { startTime = GetTickCount(); while ((GetTickCount() - startTime) <= busySpan[j]) ; Sleep(TOTAL_AMPLITUDE - busySpan[j]); } return 0; }
31人阅读
说明 · · · · · ·
表示其中内容是对原文的摘抄