Improve code running efficiency (4)

Source: Internet
Author: User

[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]

(7) try to use the query method, instead of the calculation method. Pay attention to analyzing empirical data.

Friends who have worked on the server-side software know that the response time of a single socket should be as short as possible, because this way we can respond to more users in a short time. If you put valuable time on some temporary calculations, it will not be a big deal. Historical experience running data is very important for us to analyze data. For example, we know that when the operating system allocates memory, the initial access requirements are chaotic. After a period of operation, our memory will be filled with more memory fragments. At this moment, it is difficult to obtain a large memory. In order to obtain the memory we need, the operating system has to temporarily put the memory that is temporarily not used in the operating system on the hard disk, and load it again when it is re-needed, this increases the running time and reduces the running efficiency. If we can get inspiration from experience data, we should know how to allocate data when the machine starts up, and realize the optimal state of the machine in a short time, we can save time, it also improves user satisfaction.


(8) Rational Use of IF-else statements
For example, the following statement exists:

 
If (A & B ){/*CodeProcessing */} else {/* code processing */}

We want to deal with a and B as the actual situation is more complicated. Only when both A and B are true can we enter the corresponding module for processing. However, generally, A and B appear at different frequencies. At this moment, we usually place a with a high frequency in front. Because once a fails to judge, we no longer need to judge. The result of B is meaningless for us at this time. Therefore, adjusting the order of A and B will also improve our operation efficiency. Another code similar to this code is:

 
If (A | B) {/* code processing */} else {/* code processing */}

The basic principle of the above Code is the same as that of the previous code. We need to put the common situations above, because once a is established, the judgment of Condition B becomes insignificant.

(9) use multiple pointers
The pointer is a double-edged sword. If you use it, you can get twice the result with half the effort. We can perform a small test:

 
Char * strncpy (char * DST, const char * SRC, size_t size) {char * paddress = DST; size_t COUNT = 0; if (null = DST | null = SRC | 0 = size) {return NULL;} while (count <size) {DST [count] = SRC [count]; count ++;} return paddress ;}

The above section can be written in the following form:

Char * strncpy (char * DST, const char * SRC, size_t size) {char * paddress = DST; if (null = DST | null = SRC | 0 = size) {return NULL;} while (size --> 0) {* DST ++ = * SRC ++;} return paddress ;}

No matter what type of array, it takes three steps: Starting address, offset value, and value. Pointers are direct. by increasing the number of pointers, You can omit the process of getting the first address and the Offset Value in the middle to quickly replicate the value.

(10) OptimizationAlgorithmIt is an important part to improve code efficiency.
We all know that fast sorting and heap sorting are important methods to solve the Sorting Problem, but they require a lot of memory, mainly from the uncertainty of the stack. Therefore, if the sorting function is developed and the data size is large, we have the opportunity to achieve such optimization. Another algorithm optimization is that we can reduce the running priority of tasks that are not urgent during normal operation. For example, we can take a break for a while and sleep for 10 seconds, in this way, valuable time can be left for business processing. Of course, this optimization is not so much about algorithm optimization as about process optimization.

Note later:
Optimization sacrifices code portability and reduces code readability. Code optimization is not just the methods mentioned above. In my personal experience, I think we can consider the following aspects: a) improve hardware configuration. As long as the money permits, we can greatly improve the efficiency in a short period of time. Of course, its improvement is also limited. B) the optimization process. Our code often needs to add new features, so we can add a lot of judgment, apply for a lot of resources, and even repeat some meaningless operations, this greatly reduces the running efficiency of our code. If you have a good grasp of it, repeat the functions of the project or version, the subsequent efficiency improvement is no less than the first method. This is a good way to reduce costs. Of course, the software architect must precisely grasp and control it. C) Improve the coding skills and optimize the code, which takes time and needs to be summarized. The effect of doing well is obvious, but not doing well reduces the original work efficiency.

(Full text)

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.