Test the performance improvement of final modifiers.

Source: Internet
Author: User

I accidentally saw an article saying: The final modifier can improve the performance.

So I found two ways to improve the performance:

1:
Final method:
When a method is modified with "final", it indicates that the method cannot be overwritten by the quilt class.

There are two reasons for using the final method:

1. Restrict rewriting of child classes;
Ii. Improve Execution efficiency because this situation is static binding, and Java Virtual Machine (JVM) the instant compiler does not retrieve whether this method has been overwritten in its parent class, subclass, grandfather class, grandson class, and other inline classes, this eliminates the need to dynamically determine the class to be executed (the class of the specific inline relationship.

 

Second:
If a class does not allow its subclass to overwrite a method, the final method can be declared as a final method.

There are two reasons for using the final method:

First, lock the method to prevent any inheritance class from modifying its meaning and implementation.

Second, efficient. The compiler transfers the final method to the embedded mechanism to greatly improve the execution efficiency.
According to my understanding, it is equivalent to the inline function in C ++.

 

Therefore, write my test instance:

Test 1: Non-Inheritance

Package CN. vicky. chapt12;/***** @ author Vicky. H */public class finaltest {static long temp;/*** test method 1 */Public void Method1 (int I) {temp = I + 1; temp = I + 2; temp = I + 3; temp = I + 4; temp = I + 5; temp = I + 6; temp = I + 7; temp = I + 8; temp = I + 9; temp = I + 10;}/*** test method 2 */public final void method2 (int I) {temp = I + 1; temp = I + 2; temp = I + 3; temp = I + 4; temp = I + 5; temp = I + 6; temp = I + 7; temp = I + 8; temp = I + 9; temp = I + 10;} public static void main (string ARGs []) {finaltest = new finaltest (); // The execution time of the two functions is almost the same as long start, now; Start = system. currenttimemillis (); For (INT I = 0; I <999999999; I ++) {finaltest. method1 (I);} Now = system. currenttimemillis ()-start; system. err. println ("it takes (MS ms) to call a Method1 method without the final Keyword:" + now); system. out. println ("---------------------------------------------------"); Start = system. currenttimemillis (); For (INT I = 0; I <999999999; I ++) {finaltest. method2 (I);} Now = system. currenttimemillis ()-start; system. err. println ("it takes (MS ms) to call the method2 method with the final Keyword:" + now );}}

In this case, there is almost no difference between final and non-final.

 

Test 2: Inherit

Package CN. vicky. chapt12;/***** @ author Vicky. H */public class finalparent {static long temp;/*** test method 1 */Public void Method1 (int I) {temp = I + 1; temp = I + 2; temp = I + 3; temp = I + 4; temp = I + 5; temp = I + 6; temp = I + 7; temp = I + 8; temp = I + 9; temp = I + 10;}/*** test method 2 */public final void method2 (int I) {temp = I + 1; temp = I + 2; temp = I + 3; temp = I + 4; temp = I + 5; temp = I + 6; temp = I + 7; temp = I + 8; temp = I + 9; temp = I + 10 ;}}

 

Package CN. vicky. chapt12;/*** note that this test * @ author Vicky. H */public class finalchild extends finalparent {/*** test program entry Method * @ Param ARGs entry parameter list */public static void main (string [] ARGs) {// finalchild finaltest = new finalchild (); finalparent finaltest = new finalchild (); long start, now; Start = system. currenttimemillis (); For (INT I = 0; I <999999999; I ++) {finaltest. method1 (I);} Now = system. currenttimemillis ()-start; system. err. println ("it takes (MS ms) to call a Method1 method without the final Keyword:" + now); system. out. println ("---------------------------------------------------"); Start = system. currenttimemillis (); For (INT I = 0; I <999999999; I ++) {finaltest. method2 (I);} Now = system. currenttimemillis ()-start; system. err. println ("it takes (MS ms) to call the method2 method with the final Keyword:" + now );}}

However, similarly, the execution time of functions is almost the same. Can I think it is because my code cannot quickly reach the performance impact of final? (To be continued ...)

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.