java程序中比较常见的四种判断是否为空的性能优化比较

如题所述

来做个测试  


     

java用的最多的判空,殊不知,多数人一直在用一种最耗时,性能最差的方式

本测试用例特意比较常用的4种判空形式


/**
 * 字符串判空性能大比较
 */
public class Test{

    String s = "";

    long   n = 10000000;

    //s == null || s.equals("")
    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("[s == null || s.equals(\"\")] use time: " + (endTime - startTime) + "ms");
    }

    //s == null || s.length() <= 0
    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("[s == null || s.length() <= 0] use time: " + (endTime - startTime) + "ms");
    }

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

    //s == null || s == ""
    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("[s == null || s == \"\"] use time: " + (endTime - startTime) + "ms");
    }

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

 


直接右键运行,结果一目了然



[s == null || s.equals("")] use time: 73ms

[s == null || s.length() <= 0] use time: 29ms

[s == null || s.isEmpty()] use time: 33ms

[s == null || s == ""] use time: 29ms



温馨提示:答案为网友推荐,仅供参考
第1个回答  2016-09-16

以下是java 判断字符串是否为空的四种方法:

方法一: 最多人使用的一个方法, 直观, 方便, 但效率很低:

                                    if(s == null ||"".equals(s));

方法二: 比较字符串长度, 效率高, 是我知道的最好一个方法:

                      if(s == null || s.length() <= 0);

方法三: Java SE 6.0 才开始提供的方法, 效率和方法二几乎相等, 但出于兼容性考虑, 推荐使用方法二.

                     if(s == null || s.isEmpty());

方法四: 这是一种比较直观,简便的方法,而且效率也非常的高,与方法二、三的效率差不多:

                     if (s == null || s == "");

public class Zifuchuanpankong {
    public static final String  STR = "1111111";
    public static void function1(){
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < 10000000; i++) {
            if (STR == null || "".equals(STR)) {
                ;
            }
        }
        long endTime = System.currentTimeMillis();
        System.err.println("[STR == null || \"\".equals(STR)] 耗时:"+(endTime-startTime)+" ms");
    }
    public static void function2(){
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < 10000000; i++) {
            if (STR == null || STR.length()<=0) {
                ;
            }
        }
        long endTime = System.currentTimeMillis();
        System.err.println("[STR == null || STR.length()<=0] 耗时:"+(endTime-startTime)+" ms");
    }
    public static void function3(){
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < 10000000; i++) {
            if (STR == null || STR.isEmpty()) {
                ;
            }
        }
        long endTime = System.currentTimeMillis();
        System.err.println("[STR == null || STR.isEmpty()] 耗时:"+(endTime-startTime)+" ms");
    }
    public static void function4(){
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < 10000000; i++) {
            if (STR == null || STR == "") {
                ;
            }
        }
        long endTime = System.currentTimeMillis();
        System.err.println("[STR == null || STR == \"\"] 耗时:"+(endTime-startTime)+" ms");
    }

    public static void main(String[] args) {
        function1();
        function2();
        function3();
        function4();
    }
}

[STR == null || "".equals(STR)] 耗时:51 ms
[STR == null || STR.length()<=0] 耗时:7 ms
[STR == null || STR.isEmpty()] 耗时:9 ms
[STR == null || STR == ""] 耗时:4 ms

相似回答