今天测试了一下空字符串比较的时间和==(等等于)比较的时间哪个更快,最后发现equals会比较快,
我的代码是这样的,不排除其他情况,可能效果不一样,大家可以自己测试一下
equals:
package com.lxhw.common.isnull;
import com.alibaba.fastjson.JSONObject;
/**
* Created by chirszh on 2017-09-25.
*/
public class JsonRewrite extends JSONObject{
public static void main(String[] args){
long startTime = System.currentTimeMillis(); // 获取开始时间
JsonRewrite jsRewrite = new JsonRewrite();
JSONObject bbb = new JSONObject();
bbb.put("bbb","");
Double test1 = ("".equals(bbb.getString("bbb" ))?0D:bbb.getDouble("bbb"));
System.out.println("test1-->"+test1);
long endTime = System.currentTimeMillis(); // 获取结束时间
System.out.println("程序运行时间: " + (endTime - startTime) + "ms");
}
}
运行n次时间:
==:
package com.lxhw.common.isnull;
import com.alibaba.fastjson.JSONObject;
/**
* Created by chirszh on 2017-09-25.
*/
public class JsonRewrite extends JSONObject{
public static void main(String[] args){
long startTime = System.currentTimeMillis(); // 获取开始时间
JsonRewrite jsRewrite = new JsonRewrite();
JSONObject bbb = new JSONObject();
bbb.put("bbb","");
Double test2 = (bbb.getDouble("bbb" )==null?0D:bbb.getDouble("bbb"));
System.out.println("test2-->"+test2);
long endTime = System.currentTimeMillis(); // 获取结束时间
System.out.println("程序运行时间: " + (endTime - startTime) + "ms");
}
}
运行n次时间: