Beauty of programming-drawing any image for CPU usage

Source: Internet
Author: User

Chapter 1 of the beauty of programming provides an interview question:

Write oneProgramAllows you to determine the CPU usage of the Windows Task Manager. The simpler the program, the better, and the unlimited computer language. For example, you can implement the following three situations:

1. The CPU usage is fixed at 50%, which is a straight line;

2. the CPU usage is a straight line, but the specific usage is determined by the command line parameters (parameter range: 1 ~ 100 );

3. The CPU usage status is a sine curve.

The general idea is that the CPU usage is a straight line. First, you need to know what the CPU usage is,CPU usageIt is the CPU resource occupied by the program you run, indicating the running program of your machine at a certain time point. In fact, at a certain point in time, the CPU is either occupied or not occupied, that is, the CPU usage is either 1 or 0. Why is there a 50% usage, in fact, the time here refers to a period of time. This time may pass by just a few clicks on the answer, and people may feel like a time point, but for machines, this is a time period, time period. In this way, we can understand that in a period of time, the CPU usage is the time occupied by the CPU divided by the total time of this period, that is:

CPU usage = CPU usage time/total time

In the beauty of programming, the size of loop N for (INT I = 0; I <n; I ++) is calculated based on the CPU clock speed, the clock speed of my computer is 2.00 GHz, so the n size for running 1 s in for is:

2 × 109 × 2 × 5 = 8 × 108, followed by 2 indicates that the CPU executes twoCodeAnd for (INT I = 0; I <n; I ++); is converted into five pieces of assembly, so divided by five.

In order to get close to the Windows debugging time slice, take sleep (10). Then, N takes 8000000.

The specific procedure is as follows:

# Include <iostream># Include<Windows. h>Using NamespaceSTD;IntMain (){While(True){For(IntI =0; I <8000000; I ++) {;} Sleep (10);}Return 0;}

Running result:

The result is almost a straight line, but there is a large fluctuation, especially when you are still running other programs, such as dragging the mouse, the fluctuation will be larger.

The above computation has many approximation: N computation approximation; replace the number of code executed in the CPU clock cycle with the number of for () loop Assembly codes.

You can use gettickcount () to get the system time more accurately. gettickcount () gets the number of milliseconds that the system has experienced since now.

# Include <iostream> # Include <Windows. h> Using   Namespace  STD;
Int Main () {_ int64 start_time = 0 ; Int Run_time = 10 ; Int Sleep_time = Run_time; While ( True ) {Start_time = Gettickcount (); While (Gettickcount ()-start_time) <= Run_time); sleep (sleep_time );} Return 0 ;}

Running result:

The result shows that the fluctuation of a straight line is small, and gettickcount () can obtain more accurate system time.

Let's draw a sine curve for CPU usage:

The code in the beauty of programming is as follows:

# Include <windows. h> # Include <Stdlib. h> # Include <Math. h> Const   Double Split = 0.01  ;  Const   Int Count = 200  ;  Const   Double Pi = 3.13159265  ; 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 + (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  ;} 

Running result:

The following code mainly describes the meaning of the Code:

 For(IntI =0; I <count; I ++) {Busyspan [I]= (DWORD) (half + (sin (pI * radian )*Half); idlespan [I]= Interval-Busyspan [I]; radian+ =Split ;}

We know to draw a sine curve, as long as we know 0 ~ The value of CPU usage between two shards is enough, and the other is to move periodically.

The CPU usage is a sine curve. Therefore, busyspan [I]/(busyspan [I] + idlespan [I]) = Asin (∏ Ti + PHI) + B is not recommended;

We also know that busyspan [I]/(busyspan [I] + idlespan [I]) + idlespan [I]/(busyspan [I] + idlespan [I]) = 1, here (busyspan [I] + idlespan [I]) is equal to interval. Based on the above two aspects, you can choose Phi = 0; a = B = interval/2.

Period × split × COUNT = 2 period, which is exactly a period of the sine function.

Extended: How to Make CPU usage a semi-circular curve

The reference procedure is as follows:

# Include <windows. h> # Include <Stdlib. h> # Include <Math. h> Const   Double Split = 0.01  ;  Const   Int Count = 200  ;  Const   Int Interval = 300  ; Int  Main () {DWORD busyspan [count]; DWORD idlespan [count];  Double Radian = 0.0  ;  For ( Int I = 0 ; I <count; I ++ ) {Busyspan [I] = (DWORD) (interval * SQRT ( 1 -Pow (( 1 -Radian ), 2  ); 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  ;} 

Running result:

Of course, you can also make it draw more beautiful images.

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.