【发布时间】:2019-05-30 16:21:04
【问题描述】:
为什么StringUtils.isEmtpy(s)? null:s 比!StringUtils.isEmtpy(s)? s:null 慢?在我的测试中,后一个比第一个快 1 毫秒。
测试:
public static void main(String[] args) {
List<String> list = new ArrayList<String>(Arrays.asList("a", "a", "a", "a", "a", null, "a"));
Long start1 = System.nanoTime();
for (String s : list
) {
String s1 = isEmpty(s) ? null : s;
}
Long end1 = System.nanoTime();
System.out.println("null in fisrt: " + (end1 - start1));
Long start2 = System.nanoTime();
for (String s : list
) {
String s2 = !isEmpty(s) ? s : null;
}
Long end2 = System.nanoTime();
System.out.println("null in second: " + (end2 - start2));
}
//输出 首先为空:98600 秒为空:8200
【问题讨论】:
-
您的测试方法有缺陷。众所周知,微基准测试不可靠。 “数字经常欺骗我,特别是当我自己安排它们时;在这种情况下,归因于迪斯雷利的言论通常会公正而有力地适用:'谎言分为三种:谎言、该死的谎言和统计数据。'” - 马克吐温
-
这不是测试性能的方法
-
@Deadpool,您能否提供更多详细信息?谢谢
标签: java