I am referring to this discussion. I have never written any code in C or in C++ . I do not have any CS background. However I have been working as Java developer for 5 years and now I have decided to learn more about CS and do some catching up.
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- Sorting 3 numbers without branching [closed]
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
http://en.wikipedia.org/wiki/Inlining
In computing, inline expansion, or inlining, is a compiler optimization that replaces a function call site with the body of the callee. This optimization may improve time and space usage at runtime, at the possible cost of increasing the size of the final program.
Norman Maurer explains at his blog JVM and JIT inline functionality like that
Also with a warning
And you can find a very simple code example for inlining a Java code at Eva Andreasson's Java World Post. You can find the related part of post at below.
Listing 3. Caller method
Listing 4. Called method
Listing 5. Inlined method
Listing 6. After inlining, more optimizations can be applied
Inline functions are used typically in C++ header files not Java. A C++ header file usually does not contain implemented code and is considered an interface to the cpp file of the same name, which does usually contain the implemented code. It is legal to include an inline function in a header file, usually a small lightweight function. Inline functions do come at a cost, so they should not be large memory-intensive operations. For small routines the performance hit is minimal and they are more used for convenience.
The compiler optimization answers are correct. There is another usage, though - in refactoring, inlining refers to replacing a method call with the body of the method and then removing the method. See Inline Method. There are similar refactorings, such as Inline Class.
EDIT: Note that refactoring is done manually or with a tool; in either case it involves changing the source code.
When executing a given piece of code, whenever you call a standard function the execution time is slightly higher than dumping there the code contained into that function. Dumping every time the whole code contained in a function is on the other end unmainteinable because it obviously leads to a whole mess of duplication of code.
Inlining solves the performance and maintainability issue by letting you declare the function as inline (at least in C++), so that when you call that function - instead of having your app jumping around at runtime - the code in the inline function is injected at compile time every time that given function is called.
Downside of this is that - if you inline big functions which you call a lot of times - the size of your program may significantly increase (best practices suggest to do it only on small functions indeed).
As a Java developer, you generally don't have to worry about method inlining. Java's Just-in-time compiler can and will do it automatically in most places where it makes sense.
IDEs like eclipse can have a feature that allows you to inline methods at the source code level - never do this for performance, only for code readability (e.g. when you realize that the method just calls one other method without adding anything useful itself).