java效能調優(使用局部變數)

來源:互聯網
上載者:User

標籤:java   效能調優   

/*
 * 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));
 }
}

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

調優後代碼:

/*
 * 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效能調優(使用局部變數)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.