您的位置:首页 > 编程语言

编程之美第一题,参考某同事的算法和文章

2010-03-25 14:43 260 查看
Process p = Process.GetCurrentProcess();

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

double[] busySpan = new double[COUNT]; //array of busy times
double[] idleSpan = new double[COUNT]; //array of idle times
int half = INTERVAL / 2;

double radian = 0.0;
for (int i = 0; i < COUNT; i++)
{
busySpan[i] = (double)(half + (Math.Sin(PI * radian) * half));
idleSpan[i] = INTERVAL - busySpan[i];
radian += SPLIT;
}

double startTime = 0;
int j = 0;

while (true)
{
j = j % COUNT;

startTime = Environment.TickCount;

p.ProcessorAffinity = (IntPtr)0x0001;
while ((Environment.TickCount - startTime) <= busySpan[j]) ;

p.ProcessorAffinity = (IntPtr)0x0002;
while ((Environment.TickCount - startTime) <= idleSpan[j]) ;

j++;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: