cpu时间呈正弦曲线的代码

看了《编程之美》的文章后的实现
cpu时间呈正弦曲线的代码
  1. using System;
  2. using System.Threading;
  3. using System.Runtime.InteropServices;
  4. class Program
  5. {
  6.     [DllImport("Kernel32.Dll")]
  7.     extern static int SetProcessPriorityBoost(int ProcPtr, int mask);
  8.     [DllImport("Kernel32.Dll")]
  9.     extern static int GetCurrentProcess();
  10.     static void Main()
  11.     {
  12.         //SetProcessPriorityBoost(GetCurrentProcess(), 2);
  13.         const int Count = 300;
  14.         const int Interval = 100;
  15.         int[] busySpan = new int[Count];
  16.         int[] idleSpan = new int[Count];
  17.         int half = Interval / 2;
  18.         for(int i = 0;i < Count;i++)
  19.         {
  20.             busySpan[i] = (int)(Math.Sin(2d * Math.PI * i / Count) * half + half);
  21.             idleSpan[i] = Interval - busySpan[i];
  22.         }
  23.         long t = 0;
  24.         int j = 0;
  25.         while(true)
  26.         {
  27.             t = DateTime.Now.Ticks;
  28.             while((DateTime.Now.Ticks - t) <= busySpan[j] * 10000)
  29.                 ;
  30.             Thread.Sleep(idleSpan[j]);
  31.             j = ++j % Count;
  32.         }
  33.     }
  34. }

在自己的机器上是画出来了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值