简体   繁体   English

Java 8 forEach Stream()vs old forEach Loop

[英]Java 8 forEach Stream() vs old forEach Loop

I was trying a example code with spring. 我正在尝试使用spring的示例代码。 And a part of code is like below; 代码的一部分如下所示;

private List<Point> points;
long timeTakeninMilis = System.currentTimeMillis();

public List<Point> getPoints() {
    return points;
}

public void setPoints(List<Point> points) {
    this.points = points;
}

public void drawJava8() {
    points.stream().forEachOrdered(
            point -> System.out.println("Point : (" + point.getX() + ", "
                    + point.getY() + ")"));
    System.out.println("Total Time Taken drawJava8(): "
            + (System.currentTimeMillis() - timeTakeninMilis)
            + " miliseconds");
}

public void draw() {
    for (Point point : points) {
        System.out.println("Point = (" + point.getX() + ", " + point.getY()
                + " )");

    }
    System.out.println("Total Time Taken draw(): "
            + (System.currentTimeMillis() - timeTakeninMilis)
            + " miliseconds");
}

The OUTPUT, 输出,

  Jun 30, 2015 11:30:53 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
  INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7daf6ecc: startup date [Tue Jun 30 11:30:53 IST 2015]; root of context hierarchy
  Jun 30, 2015 11:30:53 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
  INFO: Loading XML bean definitions from class path resource [spring.xml]
  Point = (0, 0 )
  Point = (-50, 0 )
  Point = (0, 50 )
  Total Time Taken draw(): 70 miliseconds
  Point : (0, 0)
  Point : (-50, 0)
  Point : (0, 50)
  Total Time Taken drawJava8(): 124 miliseconds
  Jun 30, 2015 11:30:54 AM org.springframework.context.support.ClassPathXmlApplicationContext doClose
  INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@7daf6ecc: startup date [Tue Jun 30 11:30:53 IST 2015]; root of context hierarchy

Why it is taking more time? 为什么需要更多时间? Or i am doing something wrong? 或者我做错了什么?

I was expecting it to be faster or of similar speed... Please help me understand what is the benefit of the Lambda Expressions? 我期待它更快或类似的速度......请帮助我理解Lambda表达式的好处是什么?

INFO: I did it in two different programs. 信息:我在两个不同的计划中做到了。 Times are taken from those. 时间取自那些。 I merged them here to make it short. 我在这里合并它们以缩短它们。

Adding this as an analysis per original poster's request. 根据原始海报的要求添加此分析。

We can not really predict the sophisticated analysis and transformation that the modern JIT compiler performs on running code. 我们无法真正预测现代JIT编译器在运行代码时执行的复杂分析和转换。 Hence while benchmarking items such as these, you should not conclude it just by running two method calls. 因此,在对诸如此类的项进行基准测试时,您不应仅仅通过运行两个方法调用来结束它。

Instead, create various sample input sets (boundary cases) and check the perofmance by repeatedly calling your test cases without shutting down JVM. 相反,创建各种样本输入集(边界情况)并通过反复调用测试用例而不关闭JVM来检查性能。 In this case for example : 在这种情况下,例如:

for (int i=0;i<100;i++){draw(); drawJava8();}

Once you have the results, find out average execution and you can safely ignore first execution result as it might not have had optimizations. 获得结果后,找出平均执行情况,您可以安全地忽略第一个执行结果,因为它可能没有优化。

So the conclusion you have drawn from your tests is not completely correct. 因此,您从测试中得出的结论并不完全正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM