Java Program Performance Optimization summary--------variable and function call Chapter __ Block chain

Source: Internet
Author: User
Tags int size
Use of Variables

1. Use local variables as much as possible, the parameters passed when the method is invoked, and the temporary variables created in the call are saved in the stack (stack) faster. Other variables, such as static variables, instance variables, and so on, are created in the heap (Heap) in a slower speed.

2. Try to use static variables, that is, add modifier static, if the variable in the class does not change with his instance, can be defined as a static variable, so that all his instances share the variable.

Note: Class variable (static variable), member variable, instance variable (member variable with instance), local variable


method is called

1. Reducing unnecessary calls to methods

In Java, everything is an object, and if a method is invoked, the processor first checks to see which object the method belongs to, whether the object is valid, what type of object it belongs to, and then select the appropriate method and invoke it.

You can reduce the invocation of a method by the same method:

public void Callmethod (int i) {
if (i ==0) {
Return
}
..//Other handling
}

If called directly,

int i = 0;
...
Callmethod (i);

It would be better to write:

int i = 0;
...

if (i ==0) {
Callmethod (i);
}

Without affecting the readability and so on, several small methods can be synthesized into a large method.

2. final,private, static keyword

Adding final,private and Static keywords to the methods and variables is beneficial to compiler optimization.

The Java compiler looks for opportunities to inline all final methods, which can increase performance by an average of 50%.

(Note: An inline function is a function that the code is inserted into the caller's code.)    As with #define macros, inline functions improve execution efficiency by avoiding the overhead of being invoked, especially if it can be optimized by the compiler by invoking ("Process integration"). Inline functions are similar to macros, except that macros are overridden by a preprocessor, and inline functions are implemented through compiler control. and the inline function is the real function, only when need to use, inline function like macro expansion, so cancel the function of the parameter stack, reduce the overhead of the call. You can invoke inline functions just as you would call a function without worrying about problems that arise from dealing with macros. )
  

3. Let the Getter/setter method of accessing the variables in the instance become final


The simple Getter/setter method should be set to final, which tells the compiler that this method is not overloaded, so it can become "inlined"


Example:
Class MAF {
public void setSize (int size) {
_size = size;
}
private int _size;
}

Correct:
Class Daf_fixed {
Final public void setSize (int size) {
_size = size;
}
private int _size;
}


Related Article

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.