我有这样的方法
public void doThis(){
Predicate<String> p = (arg) -> true;
doThat(p);
}
与不使用lambda进行比较相比,这会对性能产生任何影响吗? 是否可以将此lambda表达式存储为实例变量以使其更好?
答案 0 :(得分:0)
您可以看到效果:
public static void main(String[] args) {
System.out.println("Predicate using Lamda");
System.out.println(System.currentTimeMillis());
doThis();
System.out.println(System.currentTimeMillis());
System.out.println("Predicate using Without Lamda");
System.out.println(System.currentTimeMillis());
doThisWithoutLamda();
System.out.println(System.currentTimeMillis());
System.out.println("Predicate using Lamda 100000");
System.out.println(System.currentTimeMillis());
for (int i = 0; i < 100000; i++) {
doThis();
}
System.out.println(System.currentTimeMillis());
System.out.println("Predicate using Without Lamda 100000");
System.out.println(System.currentTimeMillis());
for (int i = 0; i < 100000; i++) {
doThisWithoutLamda();
}
System.out.println(System.currentTimeMillis());
}
public static void doThis() {
Predicate<String> p = (arg) -> true;
}
public static void doThisWithoutLamda() {
Predicate<String> p = new Predicate<String>() {
@Override
public boolean evaluate(String object) {
return true;
}
};
}
输出:
Predicate using Lamda
1551087600419
1551087600491
Predicate using Without Lamda
1551087600491
1551087600492
Predicate using Lamda 100000
1551087600492
1551087600496
Predicate using Without Lamda 100000
1551087600496
1551087600500