Java Performance tuning (using local variables)

Source: Internet
Author: User

/*
* Created on Jan 29, 2015
*
*  */
Package com.zcd.test;

Import Java.sql.Timestamp;

public class Test {

private int x;
private static int staticx;

public void stackaccess (int val) {
int j = 0;
for (int i = 0; i < val; i++) {
j + = 1;
}
}

public void instanceaccess (int val) {
for (int i = 0; i < val; i++) {
x + = 1;
}
}

public void staticaccess (int val) {
int tempx=staticx;
for (int i = 0; i < val; i++) {
TEMPX + = 1;
}
}

public static void Main (string[] args) {
System.out.println (New Timestamp (System.currenttimemillis ()));
Test test=new test ();
Long T1start=system.currenttimemillis ();
Test.stackaccess (1000000000);
Long T1end=system.currenttimemillis ();
System.out.println ("time1=" + (T1end-t1start));
Long T2start=system.currenttimemillis ();
Test.instanceaccess (1000000000);
Long T2end=system.currenttimemillis ();
System.out.println ("time2=" + (T2end-t2start));
Long T3start=system.currenttimemillis ();
Test.staticaccess (1000000000);
Long T3end=system.currenttimemillis ();
System.out.println ("time3=" + (T3end-t3start));
}
}

==========================================================================

After tuning the code:

/*
* Created on Jan 29, 2015
*
* Copyright 2006 ATPCO Confidential and proprietary. All rights Reserved.
*/
Package com.zcd.test;

Import Java.sql.Timestamp;

public class Test {

private int x;
private static int staticx;

public void stackaccess (int val) {
int j = 0;
for (int i = 0; i < val; i++) {
j + = 1;
}
}

public void instanceaccess (int val) {
int tempx=x;
for (int i = 0; i < val; i++) {
TEMPX + = 1;
}
}

public void staticaccess (int val) {
int tempx=staticx;
for (int i = 0; i < val; i++) {
TEMPX + = 1;
}
}

public static void Main (string[] args) {
System.out.println (New Timestamp (System.currenttimemillis ()));
Test test=new test ();
Long T1start=system.currenttimemillis ();
Test.stackaccess (1000000000);
Long T1end=system.currenttimemillis ();
System.out.println ("time1=" + (T1end-t1start));
Long T2start=system.currenttimemillis ();
Test.instanceaccess (1000000000);
Long T2end=system.currenttimemillis ();
System.out.println ("time2=" + (T2end-t2start));
Long T3start=system.currenttimemillis ();
Test.staticaccess (1000000000);
Long T3end=system.currenttimemillis ();
System.out.println ("time3=" + (T3end-t3start));
}
}

Java Performance tuning (using local variables)

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.