Java best practices: replacing instance (class) variables with local (HEAP) variables --

Source: Internet
Author: User

 

If you frequently access variables, you need to consider where to access them. Is the variable static, stack, or instance variable of the class? Does the storage location of a variable significantly affect the performance of the code that is accessed? For example, consider the following code: Class stackvars
{
Private int instvar;
Private Static int staticvar;

// Access stack variables
Void stackaccess (INT Val)
{
Int J = 0;
For (INT I = 0; I <val; I ++)
J + = 1;
}

// Instance variables of the access class
Void instanceaccess (INT Val)
{
For (INT I = 0; I <val; I ++)
Instvar + = 1;
}

// Access static variables of the class
Void staticaccess (INT Val)
{
For (INT I = 0; I <val; I ++)
Staticvar + = 1;
}
} Each method in this Code executes the same loop and repeats the same number of times. The only difference is that each loop increments a variable of different types. The method stackaccess increments a local stack variable, instanceaccess increments an instance variable of the class, and staticaccess increments a static variable of the class. Instanceaccess and staticaccess have the same execution time. However, stackaccess is two to three times faster. It is faster to access stack variables because the JVM has fewer operations to access stack variables than to access static variables or class instance variables. JVM is a stack-based virtual machine that optimizes the access and processing of stack data. All local variables are stored in a local variable table, which is processed in the Java operand stack and can be efficiently accessed. It is more costly to access static variables and instance variables because the JVM must use a more expensive operating code and access them from the constant storage pool. (The constant storage pool stores symbolic references of all types, fields, and methods used by a type .) Generally, after the static variable or instance variable is accessed from the constant storage pool for the first time, JVM dynamically changes the bytecode to use a more efficient operating code. Despite this optimization, the access to stack variables is still faster. With these facts in mind, you can rebuild the previous code to make the operation more efficient by accessing the stack variables instead of instance variables or static variables. Consider the modified Code: Class stackvars
{
// Same as the previous one...
Void instanceaccess (INT Val)
{
Int J = instvar;
For (INT I = 0; I <val; I ++)
J + = 1;
Instvar = J;
}

Void staticaccess (INT Val)
{
Int J = staticvar;
For (INT I = 0; I <val; I ++)
J + = 1;
Staticvar = J;
}
} Methods instanceaccess and staticaccess are modified to copy their instance variables or static variables to local stack variables. After the variable is processed, its value is copied back to the instance variable or static variable. This simple change significantly improves the performance of instanceaccess and staticaccess. The execution time of these three methods is basically the same. The execution speed of instanceaccess and staticaccess is only about 4% slower than that of stackaccess. This does not mean you should avoid using static variables or instance variables. You should use a storage mechanism that makes sense to your design. For example, if you access static variables or instance variables in a loop, you can temporarily store them in a local stack variable, which can significantly improve the Code performance. This will provide the most efficient bytecode instruction sequence for JVM to execute.

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.