zoukankan      html  css  js  c++  java
  • 十条改进代码性能小建议

    一,避免在循环条件中使用复杂的表达式

    在循环中,循环条件会被反复计算,如果不使用复杂表达式而使循环条件值不变的化,程序会运行的更快

    import java.util.List;
    
    public class foreach {
        void method(List list){
            for (int i = 0; i < list.size(); i++) {
                System.out.println(list.get(i));  
            }
        }
    }

    应该改为

    import java.util.List;
    
    public class foreach {
        void methodImprovement(List list){
            int size = list.size();
            for (int i = 0; i < size; i++) {
                System.out.println(list.get(i));
            }
        }
    }

    二,应该在finally块中关闭stream

    程序中使用的资源应当被释放以免造成内存泄漏,这最好放在finally块中做,不管程序运行结果怎么样,finally块一定会执行的

    import java.io.FileInputStream;
    
    public class IOStream {
        void method() {
            try {
                FileInputStream fis = new FileInputStream("xxxxxx");
                int count = 0;
                while (fis.read() != -1) {
                    count++;
                }
                System.out.println(count);
                fis.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    应当改为

    import java.io.FileInputStream;
    import java.io.IOException;
    
    public class IOStream {
        void methodImprovement() {
            FileInputStream fis = null;
            try {
                fis = new FileInputStream("xxxxxx");
                int count = 0;
                while (fis.read() != -1) {
                    count++;
                }
                System.out.println(count);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (fis != null) {
                    try {
                        fis.close();
                    } catch (IOException io) {
                      //处理异常
                    }
                }
            }
        }
    }

    三,避免不需要的instanceof操作

    如果A类继承/实现了B类/接口,那么instanceof表达式返回永远为ture

    public class Instanceof {
        public Instanceof() {
        }
    }
    
    
    class SubInstanceof extends Instanceof {
        void method() {
            SubInstanceof sub = new SubInstanceof();
            if (sub instanceof Instanceof) {
                System.out.println(true);  // 永远都是true
            }
            Instanceof ins = new Instanceof();
            if (ins instanceof Object) {
                System.out.println(true);  //永远都是true
            }
            
        }
    }

    改进

    public class Instanceof {
        public Instanceof() {
        }
    }
    
    
    class SubInstanceof extends Instanceof {
        void method() {
            SubInstanceof sub = new SubInstanceof();
            System.out.println("SubInstanceof永远是Instanceof");
            System.out.println("Instanceof永远是Object");
        }
    }

    四,避免没必要的转换类型操作

    像上面一样 任何类的实例都能用Object类型存在,同样,所有的子类也都隐含的等于其父类,由子类转向父类的操作就是不必要的了。

    五,如果只是查找单个字符,用charat()代替startwith()

    用一个字符作为参数调用startwith()也可以,但从性能上来说,使用charat()肯定更好。

    public class Demo {
        public static void main(String[] args) {
            String username="tom"; //避免魔法值
            //startWith()
            if(username.startsWith("t")){
                //方法体
            }
            //charAt()
            if(username.charAt(0)=='t'){
                //方法体
            }
        }
    }

    六,字符串相加的时候,如果该字符只有一个字符 用单引号代替双引号

    public class Strings {
        void method() {
            String a = "hell";
            System.out.println(a + "o");
        }
    }

    改进

    public class Strings {
        void method() {
            String a = "hell";
            System.out.println(a + 'o');
        }
    }

    七,尽量将try/catch移除循环

    try/catch在循环体内,会极大的影响性能

    import java.io.FileInputStream;
    
    public class IOStream2 {
        void method(FileInputStream fis) {
            for (int i = 0; i < 10; i++) {
                try {
                    int read = fis.read();
                } catch (Exception e) {
                }
            }
        }
    }

    改进

    import java.io.FileInputStream;
    
    public class IOStream2 {
        void method(FileInputStream fis) {
            try {
                for (int i = 0; i < 10; i++) {
                    int read = fis.read();
                }
            } catch (Exception e) {
            }
        }
    }

    八,对于不需要改变的字符串 用String代替StringBuff

    九,尽量使用三元运算符代替if()return;else return;

    public class TertiaryOperator {
        String method(int a) {
            if (a == 0) {
                return "true";
            } else {
                return "false";
            }
        }
    
        void method2(int a) {
            String s;
            if (a == 0) {
                s = "true";
            } else {
                s = "false";
            }
        }
    }

    改进

    public class TertiaryOperator {
        String method(int a) {
            return a == 0 ? "true" : "false";
        }
    
        void method2(int a) {
            String s;
            s = a == 0 ? "true" : "false";
        }
    }

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

    在循环里中实例化临时变量会增加内存消耗,正确的做法是在循环外实例化并反复使用

    import java.util.List;

    class Test {
    void method(List<String> names) {
    int size = names.size();
    for (int i = 0; i < size; i++) {
    Object obj = new Object();
    obj=names.get(i);
    }
    }
    }

    改进

    
    
    import java.util.List;

    class Test {
    void method(List<String> names) {
    int size = names.size();
    Object obj = new Object();
    for (int i = 0; i < size; i++) {
    obj=names.get(i);
    }
    }
    }
    不和别人一样,不复制只真正理解
  • 相关阅读:
    【Robot Framework】List 的相关使用方法
    robot framework ——关键字run keyword if 如何在一个条件下接多个执行语句,以及如何写复杂条件句-关键字Run Keywords和and
    Robotframework之页面元素操作功能
    selenium之 下拉选择框Select
    selenium修改readonly属性的元件
    Robotframework之Python的自定义库
    python 元类详解
    从<<JavaScript权威指南>>抄下来的一个例子
    链接
    Python+selenium之获取文本值和下拉框选择数据
  • 原文地址:https://www.cnblogs.com/Vinlen/p/13161568.html
Copyright © 2011-2022 走看看