Huawei software programming specification Learning (8) -- Testability
8-1: Pay attention to code efficiency during programming.
Note: code efficiency includes global efficiency, local efficiency, time efficiency, and space efficiency. Global efficiency is the system efficiency from the perspective of the entire system; Local efficiency is the efficiency from the perspective of modules or functions; time efficiency is the length of time required by the program to process input tasks; space efficiency is the memory space required by the program, such as the size of machine code space, data space, and stack space.
8-2: improve code efficiency while ensuring the correctness, stability, readability and testability of the Software System
Note: you cannot blindly pursue code efficiency, but it affects the correctness, stability, readability, and testability of the software.
8-3: Local efficiency should serve global efficiency, and it cannot affect global efficiency because of local efficiency improvement.
8-4: Improve the space efficiency by dividing the system data structure, improving the organization, and optimizing program algorithms.
Note: This is the fundamental solution to the software space efficiency.
Example: The following shows that the structure of the student's academic score is unreasonable.
typedef unsigned char BYTE;typedef unsigned short WORD;typedef struct STUDENT_SCORE_STRU{ BYTE name[8]; BYTE age; BYTE sex; BYTE class; BYTE subject; float score;} STUDENT_SCORE;
Because each student has scores for multiple subjects, the above structure will occupy a large space. The following improvements should be made (divided into two structures). The total storage space will decrease and operations will become more convenient.
typedef struct STUDENT_STRU{ BYTE name[8]; BYTE age; BYTE sex; BYTE class;} STUDENT;typedef struct STUDENT_SCORE_STRU{ WORD student_index; BYTE subject; float score;} STUDENT_SCORE;
8-5: Minimum cyclic body workload
Note: You should carefully consider whether statements in the loop body can be placed outside the loop body to minimize the workload in the loop body, thus improving the time efficiency of the program.
Example: The following code is inefficient.
for (ind = 0; ind <MAX_ADD_NUMBER; ind++){ sum += ind; back_sum = sum; /* backup sum */}
The statement "back_sum = sum;" can be placed after the for statement, as shown below.
for (ind = 0; ind <MAX_ADD_NUMBER; ind++){ sum += ind;}back_sum = sum;/* backup sum */
Others
8-1: carefully analyze and optimize Related Algorithms
8-2: carefully examine and analyze the methods in which the system and modules process inputs (such as transactions and messages) and improve the processing efficiency.
8-3: Analyze and optimize the division and organization of functions in the module, improve the organizational structure of functions in the module, and improve program efficiency
Note: the efficiency of the software system is mainly related to algorithms, task processing methods, system functions, and function structures. It generally cannot solve the fundamental problem only by working hard on the code.
8-4: always pay attention to code efficiency during programming; Be careful when optimizing code
8-5: Do not spend too much time desperately Improving the Efficiency of function code that is called less frequently.
Note: code optimization can improve efficiency, but it may cause serious consequences if it is not considered weekly.
8-6: carefully construct or directly compile functions with frequent calls or extremely high performance requirements using assembler
Note: You can use the Assembly embedding method only when you are familiar with the method in which the compilation system generates machine code and the hardware system. Embedded Assembly can improve the time and space efficiency, but there are also some risks.
8-7: To ensure program quality, increase space efficiency by compressing the amount of code, removing unnecessary code, and reducing unnecessary local and global variables
Note: This method can play a role in improving space efficiency, but it often cannot solve the fundamental problem.
8-8: in multiple cycles, the busiest cycle should be placed in the innermost layer.
Note: Reduce the number of times the CPU is switched into the cycle layer.
Example: The following code is inefficient.
for (row = 0; row< 100; row++){ for (col = 0; col < 5; col++) { sum += a[row][col]; }}
You can change it to the following method to improve efficiency.
for (col = 0; col< 5; col++){ for (row = 0; row < 100; row++) { sum += a[row][col]; }}
8-9: Reduce the number of nested loops as much as possible
8-10: Avoid containing judgment statements in the loop body, and place the loop statements in the code block of the judgment statement.
Note:The purpose is to reduce the number of judgments.The judgment statement in the cyclic body can be moved to the circulating body. Generally, the judgment statement irrelevant to the cyclic variable can be moved to the circulating body according to the specific circumstances of the program, however, you cannot.
Example: The following code is less efficient.
for (ind = 0; ind < MAX_RECT_NUMBER; ind++){ if(data_type == RECT_AREA) { area_sum += rect_area[ind]; } else { rect_length_sum += rect[ind].length; rect_width_sum += rect[ind].width; }}
Because the judgment statement is irrelevant to the loop variable, the following improvements can be made to reduce the number of judgments.
if (data_type ==RECT_AREA){ for (ind = 0; ind < MAX_RECT_NUMBER; ind++) { area_sum += rect_area[ind]; }}else{ for (ind = 0; ind < MAX_RECT_NUMBER; ind++) { rect_length_sum += rect[ind].length; rect_width_sum += rect[ind].width; }}
8-11: Try to use multiplication or other methods instead of division, especially in floating point operations.
Note: The Floating Point Operation Division takes up a large amount of CPU resources.
Example: The following expression may occupy a large amount of CPU resources.
#define PAI 3.1416radius =circle_length / (2 * PAI);
Change the floating-point division to floating-point multiplication as follows.
# Define pai_reciprocal (1/3.1416) // The specific floating point radius = circle_length * pai_reciprocal/2 will be generated during compiler compilation;
8-12: Do not blindly pursue compact code
Note: Compact Code does not represent efficient machine code.