Java 非null 判断性能对比

插入代码几次,显示格式老是太乱。

package cn.com.bugyun.tmp;

public class Test {

    String s = "";
    long n = 1000000000;

    private void function1() {
        long startTime = System.currentTimeMillis();
        for (long i = 0; i < n; i++) {
            if (s == null || s.equals(""));
        }
        long endTime = System.currentTimeMillis();
        System.out.println("function 1 use time: " + (endTime - startTime)
                + "ms");
    }

    private void function2() {
        long startTime = System.currentTimeMillis();
        for (long i = 0; i < n; i++) {
            if (s == null || s.length() <= 0);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("function 2 use time: " + (endTime - startTime)
                + "ms");
    }

    private void function3() {
        long startTime = System.currentTimeMillis();
        for (long i = 0; i < n; i++) {
            if (s == null || s.isEmpty());
        }
        long endTime = System.currentTimeMillis();
        System.out.println("function 3 use time: " + (endTime - startTime)
                + "ms");
    }

    private void function4() {
        long startTime = System.currentTimeMillis();
        for (long i = 0; i < n; i++) {
            if (s == null || s == "");
        }
        long endTime = System.currentTimeMillis();
        System.out.println("function 4 use time: " + (endTime - startTime)
                + "ms");
    }

    public static void main(String[] args) {
        Test test = new Test();
        test.function1();
        test.function2();
        test.function3();
        test.function4();
    }
}

显示结果如下:

 

猜你喜欢

转载自bugyun.iteye.com/blog/2227700