In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to optimize Linux programming code". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to optimize Linux programming code"!
Global variable VS function parameters
Global variables are often used in driver programming under Linux, such as the interrupt service function ISR, which can basically use global variables to update data and trigger events. Passing parameters to a function is generally less efficient than directly using global variables, especially when there are too many parameters. Parameter transfer, it is necessary to push the parameters to stack, wait for the program to run, and then pull out of stack, the process is more complex than global variable. However, if there are too many global variables, it will be designed to the problem of management, and it will be difficult to control the modification of these variables, which has a negative impact on the modularization of the function.
I'm sorry, I'm sorry.
These three statements are usually quoted in for () or while (), which is 3 "2" 1 in terms of execution efficiency.
This is related to the resulting assembly language, and in general, the impact is not too exaggerated.
Switch-case statement
Switch-case statement, is also used a lot of statements.
The first optimization principle here is to put the condition of occurrence probability in the position of judgment. In this way, we can effectively reduce the number of comparisons and achieve better results. Or, a better way to replace switch-case statements is to use table lookups.
such as
Switch (msg_type) {case DATA: handle_data_fun (); break; case RTS: handle_rts_fun (); break;. }
Here you can use function pointers and look up tables to get handle_fun instead.
The handling functions are all in the process of initialization and are placed in the corresponding position of * handle_ [Max].
Int (* handle_ Fun [Max]) (struct msg_t * msg); int msg_type = msg- > type; handle_ Fun [MSG _ type] (msg); / /
This method is widely used in the network protocol of Linux, and the corresponding look-up method is hash table structure.
The use of sturct and union
When using sturcut, be sure to consider the problem of bytes to it. In struct, different arrangement order, sizeof is different, so it is necessary to adjust the position to ensure that the size of struct is as minimum as possible.
The use of union, such as the data structure of different packages, is different, so try to use union to distinguish different package structures so that it is more readable to the program.
The use of volatile
This is a very useful sign in driver programming. The goal is to keep the compiler compiler from optimizing the current variables. Especially when it comes to the value of the hardware register.
Simplification of multiplication and division
Multiplication and division is too deadly for arm. Multiplication and division of arm can only be completed by generating a pile of assembly instructions. Therefore, it is generally possible to simplify properly through bit operations, such as shifting and taking low bits. For example, axiom 8 = a
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.