zoukankan      html  css  js  c++  java
  • 常见的几种改善代码效率的技巧

    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()
  • 相关阅读:
    【转载】懵逼了吧!年纪轻轻的存个屁钱啊
    【转载】CSS3的calc()使用
    【原创】修复ios输入框获取焦点时不支持fixed的bug
    【转载】Retina屏的移动设备如何实现真正1px的线?
    hadoop map(分片)数量确定
    使用DFS求任意两点的所有路径
    求图中某点到另一点的通路
    使用Maven下载jar包
    斜杠、反斜杠以及换行符
    N进制加法
  • 原文地址:https://www.cnblogs.com/accumulating/p/11846768.html
Copyright © 2011-2022 走看看