Any round of CPU into a straight round

Source: Internet
Author: User
// Any round of the entire cup straight forward
Static void getusage (float level)
{
Performancecounter P = new performancecounter ("processor", "% processor time", "_ total ");
If (P = NULL)
{
Return;
}

While (true)
{
If (P. nextvalue ()> level)
{
System. Threading. thread. Sleep (20 );
}
}

}

PS. The program displays 50% on the current machine.

 

/// <Summary>
///
/// </Summary>
/// <Param name = "cuphz"> cup Hz, for example: 2.53 </param>
/// <Param name = "ratio"> CPU usage percentage, for example, 0.8 </param>
Static void getusage_1 (float cuphz, float ratio)
{
Double R = (1-ratio) * 10;
Double Hz = (cuphz * Math. Pow (10d, 9d) * 2)/R;
For (;;)
{
For (INT I = 0; I <Hz/1000; I ++)
{

}

System. Threading. thread. Sleep (15 );
}
}
PS. Accuracy for 0.5 (50%) or above. You need to know the Working Quota of the local cup.
 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.