Windows program efficiency

zhaozj2021-02-08  223

Windows

It is a huge feature-rich system, and provides us with a powerful application interface that enables us to write a rich application, but

Windows

The complexity and the power of the application interface, we often neglect a lot of technical details, making the programs written less efficient, lack of competitiveness. Here is some rules that I have summarized to improve program efficiency, I hope to help improve the quality of the program.

Reduce the switch between the program in the core state and user state

We know that applications in the Windows system can run - user state and core state in two states, but if the process switches between two states, you need to spend thousands of CPU cycles, which is for high quality programs. It is absolutely unbearable. A typical example is the critical area and mutex (MUTEX) of user thread mutual exclusive, and the mutex is a core state object. When the application uses it, it will switch to the core state, and the critical area will work directly at the user, so If possible, it should be replaced with a mutex in a critical region.

Use thread replace the process to reduce the overhead of system resources

In a Windows system, the process is a non-matching unit, and the thread is the unit of scheduling. Because the thread sharing process address control, the creation thread can save a lot of system resource overhead compared to the creation process. Therefore, for concurrent calculations, you can use the thread mechanism as much as possible.

Reduce the overhead of the switching of the thread scene

Threads provide concurrent mechanisms for applications, allowing multiple code segments of the program to "simultaneously" running, such as the use of multiple threads to increase the cost of the IO operation, but is not the thread more and better? Of course, the thread uses the process address space, but there is still its own operating environment (stack, temporary token, etc.), the switching of thread scenes is to consume the CPU resource, the same calculation process (not involved in IO operation), use two A thread is done than using a thread to do what you need is actually more.

Reduce the cross-border (page) accesses

In the Windows system, memory is organized in a page (4KB). Since Windows is a virtual storage system, the page in memory is not used to switch to the outside, and then loaded into memory, this can Enables an application to use a maximum address range than the actual memory. However, the internal exchange in exchange is a very time-consuming IO operation compared to the CPU. Below, there are two almost exactly the same procedures. After compiling under the VC, the execution time of the previous paragraph is more than 200 times in the next paragraph.

Program 1:

Char BUF [8] [4000];

CHAR CH;

INT I, J;

For (j = 0; J <4000; J )

{

For (i = 0; i <8; j )

{

CH = BUF [I] [J];

}

}

Program 2:

Char BUF [8] [4000];

CHAR CH;

INT I, J;

For (i = 0; i <8; j )

{

For (j = 0; J <4000; J )

{

CH = BUF [i] [j];

}

}

转载请注明原文地址:https://www.9cbs.com/read-3045.html

New Post(0)