package com.dt5000.homework.action;
public class StringTest {
private static String str = "";
private static long time = 10000000;
public static void main(String[] args) {
// TODO Auto-generated method stub
f1();
//f2();
//f3();
//f4();
//f5();
//f6();
}
public static void f1() {
long start = System.currentTimeMillis();
for (int i = 0; i < time; i++) {
if(str == null || "".equals(str)){
}
}
long end = System.currentTimeMillis();
System.out.println(end - start);
}
public static void f2() {
long start = System.currentTimeMillis();
for (int i = 0; i < time; i++) {
if(str == null || str.length() <= 0){
}
}
long end = System.currentTimeMillis();
System.out.println(end - start);
}
public static void f3() {
long start = System.currentTimeMillis();
for (int i = 0; i < time; i++) {
if(str == null || str.isEmpty()){
}
}
long end = System.currentTimeMillis();
System.out.println(end - start);
}
public static void f4() {
long start = System.currentTimeMillis();
for (int i = 0; i < time; i++) {
if(str == null || str == ""){
}
}
long end = System.currentTimeMillis();
System.out.println(end - start);
}
public static void f5() {
long start = System.currentTimeMillis();
for (int i = 0; i < time; i++) {
if(StringUtil.isEmpty(str)){
}
}
long end = System.currentTimeMillis();
System.out.println(end - start);
}
public static void f6() {
long start = System.currentTimeMillis();
for (int i = 0; i < time; i++) {
if(StringUtil.isBlank(str)){
}
}
long end = System.currentTimeMillis();
System.out.println(end - start);
}
public static void f7() {
long start = System.currentTimeMillis();
for (int i = 0; i < time; i++) {
if(org.apache.commons.lang.StringUtils.isEmpty(str)){
}
}
long end = System.currentTimeMillis();
System.out.println(end - start);
}
public static void f8() {
long start = System.currentTimeMillis();
for (int i = 0; i < time; i++) {
if(org.apache.commons.lang.StringUtils.isBlank(str)){
}
}
long end = System.currentTimeMillis();
System.out.println(end - start);
}
}
首先,看上面的测试类,使用了8中不同的测试方法:前四种都很常用,第五种的StringUtil是我抽取org.apache.commons.lang.StringUtils包的两个方法isEmpty、isBlank:代码如下:
package com.dt5000.homework.action;
/**
* @author wangpj ISchoole com.dt5000.ischool.util 2012-11-19 StringUtil.java
* TODO:抽取了org.apache.commons.lang.StringUtils工具类的部分代码 下载地址:
* http://commons.apache.org/lang/download_lang.cgi
*/
public class StringUtil {
/**
* <p>
* Checks if a CharSequence is empty ("") or null.
* </p>
*
* <pre>
* StringUtils.isEmpty(null) = true
* StringUtils.isEmpty("") = true
* StringUtils.isEmpty(" ") = false
* StringUtils.isEmpty("bob") = false
* StringUtils.isEmpty(" bob ") = false
* </pre>
*
* <p>
* NOTE: This method changed in Lang version 2.0. It no longer trims the
* CharSequence. That functionality is available in isBlank().
* </p>
*
* @param cs
* the CharSequence to check, may be null
* @return {@code true} if the CharSequence is empty or null
* @since 3.0 Changed signature from isEmpty(String) to
* isEmpty(CharSequence)
*/
public static boolean isEmpty(CharSequence cs) {
return cs == null || cs.length() == 0;
}
/**
* <p>
* Checks if a CharSequence is not empty ("") and not null.
* </p>
*
* <pre>
* StringUtils.isNotEmpty(null) = false
* StringUtils.isNotEmpty("") = false
* StringUtils.isNotEmpty(" ") = true
* StringUtils.isNotEmpty("bob") = true
* StringUtils.isNotEmpty(" bob ") = true
* </pre>
*
* @param cs
* the CharSequence to check, may be null
* @return {@code true} if the CharSequence is not empty and not null
* @since 3.0 Changed signature from isNotEmpty(String) to
* isNotEmpty(CharSequence)
*/
public static boolean isNotEmpty(CharSequence cs) {
return !StringUtil.isEmpty(cs);
}
/**
* <p>
* Checks if a CharSequence is whitespace, empty ("") or null.
* </p>
*
* <pre>
* StringUtils.isBlank(null) = true
* StringUtils.isBlank("") = true
* StringUtils.isBlank(" ") = true
* StringUtils.isBlank("bob") = false
* StringUtils.isBlank(" bob ") = false
* </pre>
*
* @param cs
* the CharSequence to check, may be null
* @return {@code true} if the CharSequence is null, empty or whitespace
* @since 2.0
* @since 3.0 Changed signature from isBlank(String) to
* isBlank(CharSequence)
*/
public static boolean isBlank(CharSequence cs) {
int strLen;
if (cs == null || (strLen = cs.length()) == 0) {
return true;
}
for (int i = 0; i < strLen; i++) {
if (Character.isWhitespace(cs.charAt(i)) == false) {
return false;
}
}
return true;
}
/**
* <p>
* Checks if a CharSequence is not empty (""), not null and not whitespace
* only.
* </p>
*
* <pre>
* StringUtils.isNotBlank(null) = false
* StringUtils.isNotBlank("") = false
* StringUtils.isNotBlank(" ") = false
* StringUtils.isNotBlank("bob") = true
* StringUtils.isNotBlank(" bob ") = true
* </pre>
*
* @param cs
* the CharSequence to check, may be null
* @return {@code true} if the CharSequence is not empty and not null and
* not whitespace
* @since 2.0
* @since 3.0 Changed signature from isNotBlank(String) to
* isNotBlank(CharSequence)
*/
public static boolean isNotBlank(CharSequence cs) {
return !StringUtil.isBlank(cs);
}
}
- f1():运行结果:12ms
- f2():运行结果:8ms
- f3():运行结果:8ms
- f4():运行结果:8ms
- f5():运行结果:9~10ms
- f6():运行结果:9~10ms
- f7():运行结果:54ms
- f8():运行结果:48ms
- 方法一: 最多人使用的一个方法, 直观, 方便, 但效率较低.
- 方法二: 比较字符串长度, 效率高, 比较推荐的方法.
- 方法三: java SE 6.0 才开始提供的办法, 效率和方法二基本上相等, 但出于兼容性考虑, 推荐使用方法二或方法四.
- 方法四: 这是种最直观,简便的方法,而且效率也非常的高,与方法二、三的效率差不多
- 方法五: 抽出了org.apache.commons.lang.StringUtils的方法,减少了类中的方法和属性,反而增加了效率
- 方法六:同上
- 方法七:原声的StringUtils类方法,这个类比较大,或许时间就消耗于此,
- 方法八:同上
在做android的项目中和web项目中,比较频繁的使用了StringUtils这个类比较字符串是否为空,但个人总结了一下,使用最多的也就是这两个方法,所以建议还是抽取一定的方法来使用,当然,个人电脑配置或许不同,测试结果或许也有一定的偏差,纯属个人意见。仅供参考。