Algorithm Research-CPU usage Curve

Source: Internet
Author: User

1. The CPU usage curve is a sine function curve:

Step 1: To set the CPU usage curve to a function curve, first understand the relationship between the Function Curve and the CPU usage curve. Function y = f (x ), corresponding to the CPU usage curve, x indicates the elapsed time, and y indicates the CPU usage. We know that the CPU usage is 0%-100%, that is to say, the value range of the function is [0-1], but the value range of the sine function is [-]. Therefore, the sine function must be transformed to []. after y = 1/2 + 1/2sinx transformation, the value range is.

Step 2: For CPU usage y = 1/2 + 1/2 * sinx, that is to say, at a certain time point x, the CPU usage time is the ratio of busy time to total time y;

By following these two points, the algorithm for calculating the CPU usage curve as the sine function curve can be easily solved.

 

The Code is as follows:

# Include "Windows. h"

# Include "stdlib. h"

# Include "math. h"

 

Constdouble SPLIT = 0.01;

Const int COUNT = 100;

Const double PI = 3.14159265;

Const int INTERVAL = 300;

 

Int main ()

{

DWORD busySpan [COUNT];

DWORD idleSpan [COUNT];

Int half = INTERVAL/2;

Double radian = 0.0;

For (int I = 0; I <COUNT; I ++)

{

BusySpan [I] = DWORD (half + half * sin (PI * radian); // y = 1/2 + 1/2 * (sinx)

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;

}

 

 

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.