Is it really effective to use the final keyword inline compiler optimization in Java?

Source: Internet
Author: User

(inkfish original, reprint please indicate the source: http://blog.csdn.net/inkfish/)

In order to seek the performance optimization of Java code, search from the internet to use final keyword compile-time inline optimization method, but really effective. The actual test found that not necessarily, even the performance impact of the huge, eventually abandoned the idea of using final optimization.

Test environment: Windows XP sp2,jdk 1.6.0_15-b03,eclipse 3.5 SR1.

Package test; public class Test {public static void main (string[] args) {int loop = 10000000;//Test 10 million loop String num = "0";//----First time-- --{System.out.print ("first time:/T"); Long start = System.currenttimemillis (); for (int i = 0; I < loop; i++) {Class1 C1 = New Class1 (); C1.SETF1 (num); C1.GETF1 (); } System.out.printf ("%5dms", (System.currenttimemillis ()-start); Start = System.currenttimemillis (); for (int i = 0; I < loop; i++) {Class2 C2 = new Class2 (); C2.SETF1 (num); C2.GETF1 ();} System.out.printf ("%5dms", (System.currenttimemillis ()-start); Start = System.currenttimemillis (); Class1 C1 = new Class1 (); for (int i = 0; I < loop; i++) {C1.SETF1 (num); C1.GETF1 ();} System.out.printf ("%5dms", (System.currenttimemillis ()-start); Start = System.currenttimemillis (); Class2 C2 = new Class2 (); for (int i = 0; I < loop; i++) {C2.SETF1 (num); C2.GETF1 ();} System.out.printf ("%5dms", (System.currenttimemillis ()-start); }//----second----{System.out.print ("N/a second:/T"); long sTart = System.currenttimemillis (); for (int i = 0; I < loop; i++) {Class1 C1 = new Class1 (); C1.SETF1 (num); C1.GETF1 ();} System.out.printf ("%5dms", (System.currenttimemillis ()-start); Start = System.currenttimemillis (); for (int i = 0; I < loop; i++) {Class2 C2 = new Class2 (); C2.SETF1 (num); C2.GETF1 ();} System.out.printf ("%5dms", (System.currenttimemillis ()-start); Start = System.currenttimemillis (); Class1 C1 = new Class1 (); for (int i = 0; I < loop; i++) {C1.SETF1 (num); C1.GETF1 ();} System.out.printf ("%5dms", (System.currenttimemillis ()-start); Start = System.currenttimemillis (); Class2 C2 = new Class2 (); for (int i = 0; I < loop; i++) {C2.SETF1 (num); C2.GETF1 ();} System.out.printf ("%5dms", (System.currenttimemillis ()-start); }//----third----{System.out.print ("N/T Third:/T"); Long start = System.currenttimemillis (); for (int i = 0; I < loop; i++) {Class1 C1 = new Class1 (); C1.SETF1 (num); C1.GETF1 ();} System.out.printf ("%5dms", System.currenttimemilLis ()-start); Start = System.currenttimemillis (); for (int i = 0; I < loop; i++) {Class2 C2 = new Class2 (); C2.SETF1 (num); C2.GETF1 ();} System.out.printf ("%5dms", (System.currenttimemillis ()-start); Start = System.currenttimemillis (); Class1 C1 = new Class1 (); for (int i = 0; I < loop; i++) {C1.SETF1 (num); C1.GETF1 ();} System.out.printf ("%5dms", (System.currenttimemillis ()-start); Start = System.currenttimemillis (); Class2 C2 = new Class2 (); for (int i = 0; I < loop; i++) {C2.SETF1 (num); C2.GETF1 ();} System.out.printf ("%5dms", (System.currenttimemillis ()-start); }} class Class1 {private string F1 public string getF1 () {return f1;} public void SetF1 (String f1) {this.f1 = F1; Final class Class2 {private string F1; public final String getF1 () {return F1;} public final void setF1 (String F1) {this.f1 = F1;} }

Run Result:

First time: 110ms 1203ms 16ms 156ms Second: 94ms 1203ms 15ms 141ms Third: 94ms 1203ms 15ms 141ms

Summarize:

1. The use of final inline optimization is ineffective;

2. Sometimes the results are often unexpected.

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.