示例
package com.dong.session.pojo; public class StringIsEmpty { static String s = ""; static long n = 100000000; //方法一 private static void equals() { long startTime = System.currentTimeMillis(); for(long i = 0; i<n; i++) { if(s == null || s.equals("")); } long endTime = System.currentTimeMillis(); System.out.println("equals use time: "+ (endTime - startTime) +"ms"); } //方法二 private static void length() { long startTime = System.currentTimeMillis(); for(long i = 0; i< n; i++) { if(s == null || s.length() <= 0); } long endTime = System.currentTimeMillis(); System.out.println("length use time: "+ (endTime - startTime) +"ms"); } //方法三 private static void isEmpty() { long startTime = System.currentTimeMillis(); for(long i = 0; i <n; i++) { if(s == null || s.isEmpty()); } long endTime = System.currentTimeMillis(); System.out.println("isEmpty use time: "+ (endTime - startTime) +"ms"); } public static void main(String[] args) { equals(); length(); isEmpty(); } }
equals use time: 58ms length use time: 49ms isEmpty use time: 48ms
参考文章:https://www.cnblogs.com/yy2011/archive/2011/04/18/2020111.html