Java inline functions

Inline function
before saying inline function, let us talk about the process of calling the function.

Calling a function actually transfers the execution sequence of the program to a certain address in the memory where the function is stored. After the program content of the function is executed, it returns to the place before the execution of the function. This kind of transfer operation requires that the scene be protected and the address executed before the transfer, and the scene must be restored first after the transfer, and continue execution according to the original saved address. It is usually called pushing and popping . Therefore, function calls must have a certain time and space overhead. So for those functions whose body code is not very large and frequently called, the time and space consumption will be huge.

So how to solve this performance consumption problem, this time you need to introduce inline functions. Inline function means that when the program is compiled, the compiler directly replaces the call expression of the inline function appearing in the program with the function body of the inline function. Obviously, this will not cause the problem of reverting to reverting, but because the code in the function body is substituted into the program at compile time, it will increase the amount of target program code, which in turn increases the space overhead, and it does not cost the time. It is as big as the function call, and it can be seen that it is a time saving at the expense of increasing the target code.

Use the final keyword in java  to indicate that a function is an inline function

There are two reasons for using the final method,
one is the locking method. Prevent any inherited classes from modifying and overwriting
Second, efficiency. This instruction is not required. The final keyword just tells the compiler to consider the performance improvement when compiling, and the final function can be regarded as an inline function. But in the end, what will the compiler do? The compiler will analyze the performance comparison between processing final functions as inline and not processing them as inline.

 

Previous: Summary of Java interview questions (with answers)

Next: Summary of Java basic knowledge (absolutely classic)

Guess you like

Origin blog.csdn.net/guorui_java/article/details/109388173