欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

通過Java代碼技巧改善性能

前言

站在用戶的角度思考問題,與客戶深入溝通,找到霍林郭勒網(wǎng)站設(shè)計(jì)與霍林郭勒網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:成都網(wǎng)站建設(shè)、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名注冊(cè)、網(wǎng)頁空間、企業(yè)郵箱。業(yè)務(wù)覆蓋霍林郭勒地區(qū)。

程序的性能受到代碼質(zhì)量的直接影響。這次主要介紹一些代碼編寫的小技巧和慣例。雖然看起來有些是微不足道的編程技巧,卻可能為系統(tǒng)性能帶來成倍的提升,因此還是值得關(guān)注的。

慎用異常

在Java開發(fā)中,經(jīng)常使用try-catch進(jìn)行錯(cuò)誤捕獲,但是try-catch語句對(duì)系統(tǒng)性能而言是非常糟糕的。雖然一次try-catch中,無法察覺到她對(duì)性能帶來的損失,但是一旦try-catch語句被應(yīng)用于循環(huán)或是遍歷體內(nèi),就會(huì)給系統(tǒng)性能帶來極大的傷害。

以下是一段將try-catch應(yīng)用于循環(huán)體內(nèi)的示例代碼:

@Test
  public void test11() {

    long start = System.currentTimeMillis();
    int a = 0;
    for(int i=0;i<1000000000;i++){
      try {
        a++;
      }catch (Exception e){
        e.printStackTrace();
      }
    }
    long useTime = System.currentTimeMillis()-start;
    System.out.println("useTime:"+useTime);

  }

上面這段代碼運(yùn)行結(jié)果是:

useTime:10

下面是一段將try-catch移到循環(huán)體外的代碼,那么性能就提升了將近一半。如下:

@Test
  public void test(){
    long start = System.currentTimeMillis();
    int a = 0;
    try {
      for (int i=0;i<1000000000;i++){
        a++;
      }
    }catch (Exception e){
      e.printStackTrace();
    }
    long useTime = System.currentTimeMillis()-start;
    System.out.println(useTime);
  }

運(yùn)行結(jié)果:

useTime:6

使用局部變量

調(diào)用方法時(shí)傳遞的參數(shù)以及在調(diào)用中創(chuàng)建的臨時(shí)變量都保存在棧(Stack)中,速度快。其他變量,如靜態(tài)變量、實(shí)例變量等,都在堆(Heap)中創(chuàng)建,速度較慢。

下面是一段使用局部變量進(jìn)行計(jì)算的代碼:

@Test
  public void test11() {

    long start = System.currentTimeMillis();
    int a = 0;
    for(int i=0;i<1000000000;i++){
      a++;
    }
    long useTime = System.currentTimeMillis()-start;
    System.out.println("useTime:"+useTime);

  }

運(yùn)行結(jié)果:

useTime:5

將局部變量替換為類的靜態(tài)變量:

static int aa = 0;
  @Test
  public void test(){
    long start = System.currentTimeMillis();

    for (int i=0;i<1000000000;i++){
      aa++;
    }
    long useTime = System.currentTimeMillis()-start;
    System.out.println("useTime:"+useTime);
  }

運(yùn)行結(jié)果:

useTime:94

通過上面兩次的運(yùn)行結(jié)果,可以看出來局部變量的訪問速度遠(yuǎn)遠(yuǎn)高于類成員變量。

位運(yùn)算代替乘除法

在所有的運(yùn)算中,位運(yùn)算是最為高效的。因此,可以嘗試使用位運(yùn)算代替部分算術(shù)運(yùn)算,來提高系統(tǒng)的運(yùn)行速度。最典型的就是對(duì)于整數(shù)的乘除運(yùn)算優(yōu)化。

下面是一段使用算術(shù)運(yùn)算的代碼:

@Test
  public void test11() {

    long start = System.currentTimeMillis();
    int a = 0;
    for(int i=0;i<1000000000;i++){
      a*=2;
      a/=2;
    }
    long useTime = System.currentTimeMillis()-start;
    System.out.println("useTime:"+useTime);
  }

運(yùn)行結(jié)果:

useTime:1451

將循環(huán)體中的乘除運(yùn)算改為等價(jià)的位運(yùn)算,代碼如下:

@Test
  public void test(){
    long start = System.currentTimeMillis();
    int aa = 0;
    for (int i=0;i<1000000000;i++){
      aa<<=1;
      aa>>=1;
    }
    long useTime = System.currentTimeMillis()-start;
    System.out.println("useTime:"+useTime);
  }

運(yùn)行結(jié)果:

useTime:10

上兩段代碼執(zhí)行了完全相同的功能,在每次循環(huán)中,都將整數(shù)乘以2,并除以2。但是運(yùn)行結(jié)果耗時(shí)相差非常大,所以位運(yùn)算的效率還是顯而易見的。

提取表達(dá)式

在軟件開發(fā)過程中,程序員很容易有意無意地讓代碼做一些“重復(fù)勞動(dòng)”,在大部分情況下,由于計(jì)算機(jī)的高速運(yùn)行,這些“重復(fù)勞動(dòng)”并不會(huì)對(duì)性能構(gòu)成太大的威脅,但若希望將系統(tǒng)性能發(fā)揮到極致,提取這些“重復(fù)勞動(dòng)”相當(dāng)有意義。

比如以下代碼中進(jìn)行了兩次算術(shù)計(jì)算:

@Test
  public void testExpression(){
    long start = System.currentTimeMillis();
    double d = Math.random();
    double a = Math.random();
    double b = Math.random();
    double e = Math.random();

    double x,y;
    for(int i=0;i<10000000;i++){
      x = d*a*b/3*4*a;
      y = e*a*b/3*4*a;
    }
    long useTime = System.currentTimeMillis()-start;
    System.out.println("useTime:"+useTime);

  }

運(yùn)行結(jié)果:

useTime:21

仔細(xì)看能發(fā)現(xiàn),兩個(gè)計(jì)算表達(dá)式的后半部分完全相同,這也意味著在每次循環(huán)中,相同部分的表達(dá)式被重新計(jì)算了。

那么改進(jìn)一下后就變成了下面的樣子:

@Test
  public void testExpression99(){
    long start = System.currentTimeMillis();
    double d = Math.random();
    double a = Math.random();
    double b = Math.random();
    double e = Math.random();

    double p,x,y;
    for(int i=0;i<10000000;i++){
      p = a*b/3*4*a;
      x = d*p;
      y = e*p;
    }
    long useTime = System.currentTimeMillis()-start;
    System.out.println("useTime:"+useTime);
  }

運(yùn)行結(jié)果:

useTime:11

通過運(yùn)行結(jié)果我們可以看出來具體的優(yōu)化效果。

同理,如果在某循環(huán)中需要執(zhí)行一個(gè)耗時(shí)操作,而在循環(huán)體內(nèi),其執(zhí)行結(jié)果總是唯一的,也應(yīng)該提取到循環(huán)體外。

例如下面的代碼:

for(int i=0;i<100000;i++){
  x[i] = Math.PI*Math.sin(y)*i;
}

應(yīng)該改進(jìn)成下面的代碼:

//提取復(fù)雜,固定結(jié)果的業(yè)務(wù)邏輯處理到循環(huán)體外
double p = Math.PI*Math.sin(y);
for(int i=0;i<100000;i++){
  x[i] = p*i;
}

使用arrayCopy()

數(shù)組復(fù)制是一項(xiàng)使用頻率很高的功能,JDK中提供了一個(gè)高效的API來實(shí)現(xiàn)它。

/**
   * @param   src   the source array.
   * @param   srcPos  starting position in the source array.
   * @param   dest   the destination array.
   * @param   destPos starting position in the destination data.
   * @param   length  the number of array elements to be copied.
   * @exception IndexOutOfBoundsException if copying would cause
   *        access of data outside array bounds.
   * @exception ArrayStoreException if an element in the <code>src</code>
   *        array could not be stored into the <code>dest</code> array
   *        because of a type mismatch.
   * @exception NullPointerException if either <code>src</code> or
   *        <code>dest</code> is <code>null</code>.
   */
  public static native void arraycopy(Object src, int srcPos,
                    Object dest, int destPos,
                    int length);

如果在應(yīng)用程序中需要進(jìn)行數(shù)組復(fù)制,應(yīng)該使用這個(gè)函數(shù),而不是自己實(shí)現(xiàn)。

下面來舉例:

@Test
  public void testArrayCopy(){
    int size = 100000;
    int[] array = new int[size];
    int[] arraydest = new int[size];

    for(int i=0;i<array.length;i++){
      array[i] = i;
    }
    long start = System.currentTimeMillis();
    for (int k=0;k<1000;k++){
      //進(jìn)行復(fù)制
      System.arraycopy(array,0,arraydest,0,size);
    }
    long useTime = System.currentTimeMillis()-start;
    System.out.println("useTime:"+useTime);
  }

運(yùn)行結(jié)果:

useTime:59

相對(duì)應(yīng)地,如果在程序中,自己實(shí)現(xiàn)數(shù)組復(fù)制,其等價(jià)代碼如下:

@Test
  public void testArrayCopy99(){
    int size = 100000;
    int[] array = new int[size];
    int[] arraydest = new int[size];

    for(int i=0;i<array.length;i++){
      array[i] = i;
    }
    long start = System.currentTimeMillis();
    for (int k=0;k<1000;k++){
      for(int i=0;i<size;i++){
        arraydest[i] = array[i];
      }
    }
    long useTime = System.currentTimeMillis()-start;
    System.out.println("useTime:"+useTime);
  }

運(yùn)行結(jié)果:

useTime:102

通過運(yùn)行結(jié)果可以看出效果。

因?yàn)镾ystem.arraycopy()函數(shù)是native函數(shù),通常native函數(shù)的性能要優(yōu)于普通函數(shù)。僅出于性能考慮,在程序開發(fā)時(shí),應(yīng)盡可能調(diào)用native函數(shù)。

標(biāo)題名稱:通過Java代碼技巧改善性能
鏈接地址:http://chinadenli.net/article32/ihcdpc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、App開發(fā)、標(biāo)簽優(yōu)化網(wǎng)站導(dǎo)航、靜態(tài)網(wǎng)站商城網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作