• 常见的几种改善代码效率的技巧


    1. 尽量在循环外使用try语句捕获异常
    public class CatchTest {
        public static void main(String[] args) {
            int a = 0;
            long start = System.currentTimeMillis();
            for (int i = 0; i < 1000000000; i++) {
                try {
                    a++;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            System.out.println("innerCatch:" + (System.currentTimeMillis() - start));
    
            long start2 = System.currentTimeMillis();
            try {
                for (int i = 0; i < 1000000000; i++) {
                    a++;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("outerCatch:" + (System.currentTimeMillis() - start2));
        }
    }
    
    Sout:
    innerCatch:3
    outerCatch:2
    
    1. 能有局部变量的地方尽量不使用全局变量
    public class LocalVariableTest {
        private static int a = 2;
    
        public static void main(String[] args) {
            long start = System.currentTimeMillis();
            for (int i = 0; i < 1000000000; i++) {
                a++;
            }
            System.out.println("staticVariable:" + (System.currentTimeMillis() - start));
    
            int a = 0;
            long start2 = System.currentTimeMillis();
            for (int i = 0; i < 1000000000; i++) {
                a++;
            }
            System.out.println("localVariable:" + (System.currentTimeMillis() - start2));
        }
    
    
    }
    sout:
    staticVariable:87
    localVariable:3
    
    1. 提取公共表达式
    public class ExpressionTest {
        public static void main(String[] args) {
            double a = Math.random();
            double b = Math.random();
            double c = Math.random();
            double d = Math.random();
            double x, y;
            long start = System.currentTimeMillis();
            for (int i = 0; i < 1000000000; i++) {
                x = a * b * c / 3 * 4 * d;
                y = b * d * c / 3 * 4 * d;
            }
            System.out.println("staticVariable:" + (System.currentTimeMillis() - start));
    
            long start2 = System.currentTimeMillis();
            double z;
            for (int i = 0; i < 1000000000; i++) {
                z = c / 3 * 4 * d;
                x = a * b * z;
                y = b * d * z;
            }
            System.out.println("staticVariable:" + (System.currentTimeMillis() - start2));
        }
    }
    Sout:
    staticVariable:7
    staticVariable:2
    
    1. 采用位运算代替普通运算
    public class ByteCalcTest {
        public static void main(String[] args) {
            long start = System.currentTimeMillis();
            int a = 3;
            for (int i = 0; i < 1000000000; i++) {
                a /= 2;
                a *= 2;
            }
            System.out.println("ordinal:" + (System.currentTimeMillis() - start));
    
            long start2 = System.currentTimeMillis();
            int b = 3;
            for (int i = 0; i < 1000000000; i++) {
                b <<= 1;
                b >>= 1;
            }
            System.out.println("bit:" + (System.currentTimeMillis() - start2));
        }
    }
    sout:
    ordinal:1188
    bit:3
    
    1. 数组复制时采用System.arrayCopy()方法
    public class ArrayTest {
        public static void main(String[] args) {
            int[] a = {1, 2, 3, 4};
            long start1 = System.currentTimeMillis();
            int[] b = new int[a.length];
            for (int i = 0; i < 1000000000; i++) {
                b = a.clone();
            }
            System.out.println("Object.clone():" + (System.currentTimeMillis() - start1));
    
            long start2 = System.currentTimeMillis();
            for (int i = 0; i < 1000000000; i++) {
                System.arraycopy(a, 0, b, 0, a.length);
            }
            System.out.println("System.arraycopy():" + (System.currentTimeMillis() - start2));
    
            long start3 = System.currentTimeMillis();
            for (int i = 0; i < 1000000000; i++) {
                b = Arrays.copyOf(a, a.length);
            }
            System.out.println("Array.copyOf():" + (System.currentTimeMillis() - start3));
    
            long start4 = System.currentTimeMillis();
            for (int i = 0; i < 1000000000; i++) {
                for (int j = 0; j < a.length; j++) {
                    b[j] = a[j];
                }
            }
            System.out.println("for循环:" + (System.currentTimeMillis() - start4));
        }
    }
    sout:
    Object.clone():8141
    System.arraycopy():5326
    Array.copyOf():7356
    for循环:4260
    

    但是,如果将数组a中的数据换成一个如下对象:

    public class A {}
    

    各数组复制方法时间损耗如下:

    Object.clone():8018
    System.arraycopy():6376
    Array.copyOf():6894
    for循环:7538
    

    结论:当数组元素较简单时,各种数组复制方法的性能比较如下:

    for循环>System.arraycopy()>Arrays.copyof()>Object.clone()
    当数组元素较复杂时,各种数组复制方法的性能比较如下:
    System.arraycopy()>Arrays.copyof()>for循环>Object.clone()
  • 相关阅读:
    python操作excel表格
    重学Java
    重学Java
    重学Java
    重学Java
    重学Java
    博客园自定义主题及目录组件
    重学Java
    修改 Eclipse 背景颜色的高级方法
    dbf转excel
  • 原文地址:https://www.cnblogs.com/accumulating/p/11846768.html
Copyright © 2020-2023  润新知