新版编程之美随书源代码-中文注释.docVIP

  • 28
  • 0
  • 约6万字
  • 约 55页
  • 2018-10-06 发布于湖北
  • 举报
新版编程之美随书源代码-中文注释.doc

PAGE 22 第1章 游戏之乐——游戏中碰到的题目 天天快乐 第1章 游戏之乐 ——游戏中碰到的题目 代码清单 代码清单1-1 int main() { for(; ; { for(int i = 0; i 9600000; i++) ; Sleep(10); } return 0; } 代码清单 代码清单1-2 int busyTime = 10; // 10 ms int idleTime = busyTime; // same ratio will lead to 50% cpu usage Int64 startTime = 0; while(true) { startTime = GetTickCount(); // busy loop while((GetTickCount() - startTime) = busyTime) ; // idle loop Sleep(idleTime); } 代码清单1-3 代码清单1-3 // C# code static void MakeUsage(float level) { PerformanceCounter p = new PerformanceCounter(Processor, %Processor Time, _Total); if(p==NULL) { return } while(true) { if(p.NextValue() level) System.Threading.Thread.Sleep(10); } } 代码清单1-4 代码清单1-4 // C++ code to make task manager generate sine graph #include Windows.h #include stdlib.h #include math.h const double SPLIT = 0.01; const int COUNT = 200; const double PI = 3 const int INTERVAL = 300; int _tmain(int argc, _TCHAR* argv[]) { 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; } 代码清单1-5 代码清单1-5 _PROCESSOR_POWER_INFORMATION info; CallNTPowerInformation(11, // query processor power information NULL, // no input buffer 0, // input buffer size is zero info, //

您可能关注的文档

文档评论(0)

1亿VIP精品文档

相关文档