ホームページ  >  記事  >  Java  >  Java でのプログラムのパフォーマンス最適化の例

Java でのプログラムのパフォーマンス最適化の例

黄舟
黄舟オリジナル
2017-07-27 10:31:571130ブラウズ

1. ループ条件で複雑な式を使用しないようにします

コンパイルの最適化を行わないと、ループ条件はループ内で繰り返し計算されます。複雑な式が使用されない場合、ループ条件の値は変更されません。 、プログラムはより速く実行されます。

例:

import java.util.vector;
class cel {
    void method (vector vector) {
        for (int i = 0; i < vector.size (); i++)  // violation
            ; // ...
    }
}


修正:

class cel_fixed {
    void method (vector vector) {
        int size = vector.size ()
        for (int i = 0; i < size; i++)
            ; // ...
    }
}



2. 'vectors' と 'hashtables' の初期サイズを定義します

サイズを拡張する場合、jvm はより大きなサイズを再作成する必要がありますベクトル配列の内容をコピーし、元の配列の内容をコピーし、最後に元の配列がリサイクルされます。ベクトル容量の拡張には時間がかかることがわかります。
多くの場合、デフォルトの 10 要素サイズでは十分ではありません。必要な最適なサイズを正確に見積もったほうがよいでしょう。

例:

import java.util.vector;
public class dic {
    public void addobjects (object[] o) {
        // if length > 10, vector needs to expand
        for (int i = 0; i< o.length;i++) {    
            v.add(o);   // capacity before it can add more elements.
        }
    }
    public vector v = new vector();  // no initialcapacity.
}


修正:
初期サイズを自分で設定してください。

    public vector v = new vector(20);  
    public hashtable hash = new hashtable(10);

3.finallyブロックでストリームを閉じます

リソースリークを避けるために、プログラムで使用されているリソースを解放する必要があります。これは、finally ブロックで行うのが最適です。プログラムの実行結果に関係なく、リソースが正しく閉じられるようにするために、finally ブロックが常に実行されます。

例:

import java.io.*;
public class cs {
    public static void main (string args[]) {
        cs cs = new cs ();
        cs.method ();
    }
    public void method () {
        try {
            fileinputstream fis = new fileinputstream ("cs.java");
            int count = 0;
            while (fis.read () != -1)
                count++;
            system.out.println (count);
            fis.close ();
        } catch (filenotfoundexception e1) {
        } catch (ioexception e2) {
        }
    }
}

修正:
最後の catch の後に、finally ブロックを追加します

4. ループして配列をコピーする代わりに、「system.arraycopy ()」を使用します

'system.arraycopy ( ) ' ループを介して配列をコピーするよりもはるかに高速です。

例:

public class irb
{
    void method () {
        int[] array1 = new int [100];
        for (int i = 0; i < array1.length; i++) {
            array1 [i] = i;
        }
        int[] array2 = new int [100];
        for (int i = 0; i < array2.length; i++) {
            array2 [i] = array1 [i];                 // violation
        }
    }
}


修正:

public class irb
{
    void method () {
        int[] array1 = new int [100];
        for (int i = 0; i < array1.length; i++) {
            array1 [i] = i;
        }
        int[] array2 = new int [100];
        system.arraycopy(array1, 0, array2, 0, 100);
    }
}

5. インスタンス内の変数にアクセスする getter/setter メソッドを「final」にする

単純な getter/setter メソッドは、final にする必要があります。このメソッドはオーバーロードされないため、「インライン化」できることをコンパイラーに伝えます

例:

class maf {
    public void setsize (int size) {
         _size = size;
    }
    private int _size;
}

修正:

class daf_fixed {
    final public void setsize (int size) {
         _size = size;
    }
    private int _size;
}


6. 不必要な instanceof 操作を回避します


左側のオブジェクトが右側のオブジェクトと等しい場合、instanceof 式は常に true を返します。 ️ with with instanceof の助けを借りて。


public class uiso {
    public uiso () {}
}
class dog extends uiso {
    void method (dog dog, uiso u) {
        dog d = dog;
        if (d instanceof uiso) // always true.
            system.out.println("dog is a uiso");
        uiso uiso = u;
        if (uiso instanceof object) // always true.
            system.out.println("uiso is an object");
    }
}


7. 不必要なスタイル操作を避ける

すべてのクラスはオブジェクトから直接的または間接的に継承します。同様に、すべてのサブクラスは暗黙的にその親クラスと「等しい」です。そうすれば、サブクラスから親クラスへのモデリングという操作が不要になります。 R 例: re

class dog extends uiso {
    void method () {
        dog d;
        system.out.println ("dog is an uiso");
        system.out.println ("uiso is an uiso");
    }
}
訂正:
class unc {
    string _id = "unc";
}
class dog extends unc {
    void method () {
        dog dog = new dog ();
        unc animal = (unc)dog;  // not necessary.
        object o = (object)dog;         // not necessary.
    }
}




、単一の文字を見つけた場合は、startswith () の代わりに chart () を使用してください


パラメータとして文字を使用して、startswith ( ) ただし、パフォーマンスの観点から見ると、String API を使用して呼び出すのは間違いなく間違いです。 , 「a / b」操作の代わりにシフト操作を使用してください

「/」は非常に「高価な」操作であり、シフト操作を使用する方が高速かつ効率的になります。

例:

class dog extends unc {
    void method () {
        dog dog = new dog();
        unc animal = dog;
        object o = dog;
    }
}

修正:

public class pcts {
    private void method(string s) {
        if (s.startswith("a")) { // violation
            // ...
        }
    }
}


10. 'a * b'の代わりにshift演算を使用します 上と同じ。
[i]しかし、私は個人的に、この方法は非常に大規模なループ内で、パフォーマンスが非常に重要で、自分が何をしているのかを理解している場合を除いてのみ使用すべきだと考えています。そうしないと、パフォーマンスの向上によってプログラムの可読性が低下し、不経済になります。

例:

public class pcts {
    private void method(string s) {
        if (&#39;a&#39; == s.charat(0)) {
            // ...
        }
    }
}

修正:


public class sp {
    public static final int num = 16;
    public void calculate(int a) {
        int p = a / 4;            // should be replaced with "a >> 2".
        int p2 = a / 8;         // should be replaced with "a >> 3".
        int temp = a / 3;
    }
}

11. 文字列に文字が 1 つしか含まれていない場合は、「 」の代わりに ' を使用してください
例:

public class sp {
    public static final int num = 16;
    public void calculate(int a) {
        int p = a >> 2;  
        int p2 = a >> 3;
        int temp = a / 3;       // 不能转换成位移操作
    }
}


修正:
1つの文字列を「 」に置き換えます

public class str {
    public void method(string s) {
        string string = s + &#39;d&#39;
        string = "abc" + &#39;d&#39;   
    }
}


十二、不要在循环中调用synchronized(同步)方法

方法的同步需要消耗相当大的资料,在一个循环中调用它绝对不是一个好主意。

例子:

import java.util.vector;
public class syn {
    public synchronized void method (object o) {
    }
    private void test () {
        for (int i = 0; i < vector.size(); i++) {
            method (vector.elementat(i));    // violation
        }
    }
    private vector vector = new vector (5, 5);
}

更正:
不要在循环体中调用同步方法,如果必须同步的话,推荐以下方式:

import java.util.vector;
public class syn {
    public void method (object o) {
    }
private void test () {
    synchronized{//在一个同步块中执行非同步方法
            for (int i = 0; i < vector.size(); i++) {
                method (vector.elementat(i));   
            }
        }
    }
    private vector vector = new vector (5, 5);
}



十三、将try/catch块移出循环

把try/catch块放入循环体内,会极大的影响性能,如果编译jit被关闭或者你所使用的是一个不带jit的jvm,性能会将下降21%之多!
        
例子:        

import java.io.fileinputstream;
public class try {
    void method (fileinputstream fis) {
        for (int i = 0; i < size; i++) {
            try {                                      // violation
                _sum += fis.read();
            } catch (exception e) {}
        }
    }
    private int _sum;
}

更正:        
将try/catch块移出循环        

 void method (fileinputstream fis) {
        try {
            for (int i = 0; i < size; i++) {
                _sum += fis.read();
            }
        } catch (exception e) {}
    }

十四、对于boolean值,避免不必要的等式判断

将一个boolean值与一个true比较是一个恒等操作(直接返回该boolean变量的值). 移走对于boolean的不必要操作至少会带来2个好处:
1)代码执行的更快 (生成的字节码少了5个字节);
2)代码也会更加干净 。

例子:

public class ueq
{
    boolean method (string string) {
        return string.endswith ("a") == true;   // violation
    }
}

更正:

class ueq_fixed
{
    boolean method (string string) {
        return string.endswith ("a");
    }
}

十五、对于常量字符串,用'string' 代替 'stringbuffer'

常量字符串并不需要动态改变长度。
例子:

public class usc {
    string method () {
        stringbuffer s = new stringbuffer ("hello");
        string t = s + "world!";
        return t;
    }
}

更正:
把stringbuffer换成string,如果确定这个string不会再变的话,这将会减少运行开销提高性能。

十六、用'stringtokenizer' 代替 'indexof()' 和'substring()'

字符串的分析在很多应用中都是常见的。使用indexof()和substring()来分析字符串容易导致 stringindexoutofboundsexception。而使用stringtokenizer类来分析字符串则会容易一些,效率也会高一些。

例子:

public class ust {
    void parsestring(string string) {
        int index = 0;
        while ((index = string.indexof(".", index)) != -1) {
            system.out.println (string.substring(index, string.length()));
        }
    }
}

十七、使用条件操作符替代"if (cond) return; else return;" 结构

条件操作符更加的简捷
例子:

public class if {
    public int method(boolean isdone) {
        if (isdone) {
            return 0;
        } else {
            return 10;
        }
    }
}

更正:

public class if {
    public int method(boolean isdone) {
        return (isdone ? 0 : 10);
    }
}


十八、使用条件操作符代替"if (cond) a = b; else a = c;" 结构

例子:

public class ifas {
    void method(boolean istrue) {
        if (istrue) {
            _value = 0;
        } else {
            _value = 1;
        }
    }
    private int _value = 0;
}



更正:

public class ifas {
    void method(boolean istrue) {
        _value = (istrue ? 0 : 1);       // compact expression.
    }
    private int _value = 0;
}


十九、不要在循环体中实例化变量

在循环体中实例化临时变量将会增加内存消耗

例子:        

import java.util.vector;
public class loop {
    void method (vector v) {
        for (int i=0;i < v.size();i++) {
            object o = new object();
            o = v.elementat(i);
        }
    }
}


        
更正:        
在循环体外定义变量,并反复使用        

import java.util.vector;
public class loop {
    void method (vector v) {
        object o;
        for (int i=0;i<v.size();i++) {
            o = v.elementat(i);
        }
    }
}



二十、确定 stringbuffer的容量

stringbuffer的构造器会创建一个默认大小(通常是16)的字符数组。在使用中,如果超出这个大小,就会重新分配内存,创建一个更大的数组,并将原先的数组复制过来,再丢弃旧的数组。在大多数情况下,你可以在创建stringbuffer的时候指定大小,这样就避免了在容量不够的时候自动增长,以提高性能。

例子:        

public class rsbc {
    void method () {
        stringbuffer buffer = new stringbuffer(); // violation
        buffer.append ("hello");
    }
}

        
更正:        
为stringbuffer提供寝大小。        

public class rsbc {
    void method () {
        stringbuffer buffer = new stringbuffer(max);
        buffer.append ("hello");
    }
    private final int max = 100;
}


二十一、尽可能的使用栈变量

如果一个变量需要经常访问,那么你就需要考虑这个变量的作用域了。static? local?还是实例变量?访问静态变量和实例变量将会比访问局部变量多耗费2-3个时钟周期。
        
例子:

public class usv {
    void getsum (int[] values) {
        for (int i=0; i < value.length; i++) {
            _sum += value[i];           // violation.
        }
    }
    void getsum2 (int[] values) {
        for (int i=0; i < value.length; i++) {
            _staticsum += value[i];
        }
    }
    private int _sum;
    private static int _staticsum;
}


        
更正:        
如果可能,请使用局部变量作为你经常访问的变量。
你可以按下面的方法来修改getsum()方法:        

void getsum (int[] values) {
    int sum = _sum;  // temporary local variable.
    for (int i=0; i < value.length; i++) {
        sum += value[i];
    }
    _sum = sum;
}


二十二、不要总是使用取反操作符(!)

取反操作符(!)降低程序的可读性,所以不要总是使用。

例子:

public class dun {
    boolean method (boolean a, boolean b) {
        if (!a)
            return !a;
        else
            return !b;
    }
}


更正:
如果可能不要使用取反操作符(!)

二十三、与一个接口 进行instanceof操作


基于接口的设计通常是件好事,因为它允许有不同的实现,而又保持灵活。只要可能,对一个对象进行instanceof操作,以判断它是否某一接口要比是否某一个类要快。

例子:

public class insof {
    private void method (object o) {
        if (o instanceof interfacebase) { }  // better
        if (o instanceof classbase) { }   // worse.
    }
}

class classbase {}
interface interfacebase {}


以上がJava でのプログラムのパフォーマンス最適化の例の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。