Java 8与Java 7代码:lambda表达式表现更差

时间:2016-01-28 12:46:14

标签: java performance lambda java-8

我目前正在阅读“Java 8 in Action:Lambdas,溪流和功能式编程”这本书。由Raoul-Gabriel Urma,Mario Fusco和Alan Mycroft撰写,这个问题是关于在第2章介绍Lambda表达式的一个例子。

根据教科书示例,我编写了这些Java类。

似乎Java 8 lambda表达式的性能比使用匿名内部类编写的java8之前的代码差。

  • Java 8 lambda - 51145201 nanos
  • Plain java - 1859449 nanos

我想听听你的意见,我是否理解错误或我的测试不正确?此刻,我没有动力继续学习Java 8。

要尝试这些示例,请将这些类(Apple.java,Formatter.java,AppWithLambda.java和AppWithoutLambda.java)复制到' com.prash.parametarisedbehaviour'进入IDE并执行两个AppX主类。

package com.prash.parametarisedbehaviour;

public class Apple {
private String colour;
private Integer weight;

public Apple(String colour, int weight) {
    super();
    this.colour = colour;
    this.weight = weight;
}

public String getColour() {
    return colour;
}

public void setColour(String colour) {
    this.colour = colour;
}

public Integer getWeight() {
    return weight;
}

public void setWeight(Integer weight) {
    this.weight = weight;
}
}


package com.prash.parametarisedbehaviour;

public interface Formatter<T> {
String accept(T t);
}

package com.prash.parametarisedbehaviour;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

public class AppWithoutLambda {

public static void main(String[] args) throws IOException {
    runWithoutLambda();
}

@SuppressWarnings("unchecked")
public static <T> void runWithoutLambda() throws IOException {
    long start = System.nanoTime();
    List<T> inventory = (List<T>) Arrays.asList(new Apple("red", 100), new Apple("green", 160),
            new Apple("yellow", 170), new Apple("blue", 130));
    System.out.println("Applying weight formatter...");
    prettyPrint(inventory, new Formatter<T>() {
        public String accept(T a) {
            String characteristic = ((Apple) a).getWeight() > 150 ? "heavy" : "light";
            return "A " + characteristic + " apple.";
        }
    });
    System.out.println("Applying colour formatter...");
    prettyPrint(inventory, new Formatter<T>() {
        public String accept(T t) {
            return "A " + ((Apple) t).getColour() + " apple.";
        }
    });
    long end = System.nanoTime() - start;
    System.out.println("Time taken without lambda: " + end + " nanos");
}

public static <T> void prettyPrint(List<T> inventory, Formatter<T> formatter) {
    for (T t : inventory) {
        String output = formatter.accept(t);
        System.out.println(output);
    }
}

}

package com.prash.parametarisedbehaviour;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

public class AppWithLambda {

public static void main(String[] args) throws IOException {
    runWithLamda();
}

@SuppressWarnings("unchecked")
public static <T> void runWithLamda() throws IOException {
    long start = System.nanoTime();
    List<T> inventory = (List<T>) Arrays.asList(new Apple("red", 100), new Apple("green", 160),
            new Apple("yellow", 170), new Apple("blue", 130));
    System.out.println("Applying weight formatter...");
    prettyPrintWithStream(inventory, (T a) -> {
        String characteristic = ((Apple) a).getWeight() > 150 ? "heavy" : "light";
        return "A " + characteristic + " apple.";
    });
    System.out.println("Applying colour formatter...");
    prettyPrintWithStream(inventory, (T a) -> "A " + ((Apple) a).getColour() + " apple.");
    long end = System.nanoTime() - start;
    System.out.println("Time taken with Lambda: " + end + " nanos");

}

public static <T> void prettyPrintWithStream(List<T> inventory, Formatter<T> formatter) {
    inventory.stream().forEach((t) -> {
        String output = formatter.accept(t);
        System.out.println(output);
    });
}
}

0 个答案:

没有答案