草庐IT

java - 我应该如何用流来总结一些东西?

coder 2024-03-27 原文

我已经看到并尝试了如何在流中求和的不同实现。这是我的代码:

List<Person> persons = new ArrayList<Person>();

for(int i=0; i < 10000000; i++){
    persons.add(new Person("random", 26));
}

Long start = System.currentTimeMillis();
int test = persons.stream().collect(Collectors.summingInt(p -> p.getAge()));
Long end = System.currentTimeMillis();
System.out.println("Sum of ages = " + test + " and it took : " + (end - start) + " ms with collectors");

Long start3 = System.currentTimeMillis();
int test3 = persons.parallelStream().collect(Collectors.summingInt(p -> p.getAge()));
Long end3 = System.currentTimeMillis();
System.out.println("Sum of ages = " + test3 + " and it took : " + (end3 - start3) + " ms with collectors and parallel stream");


Long start2 = System.currentTimeMillis();
int test2 = persons.stream().mapToInt(p -> p.getAge()).sum();
Long end2 = System.currentTimeMillis();
System.out.println("Sum of ages = " + test2 + " and it took : " + (end2 - start2) + " ms with map and sum");

Long start4 = System.currentTimeMillis();
int test4 = persons.parallelStream().mapToInt(p -> p.getAge()).sum();
Long end4 = System.currentTimeMillis();
System.out.println("Sum of ages = " + test4 + " and it took : " + (end4 - start4) + " ms with map and sum and parallel stream");

这给了我以下结果:
Sum of ages = 220000000 and it took : 110 ms with collectors
Sum of ages = 220000000 and it took : 272 ms with collectors and parallel stream
Sum of ages = 220000000 and it took : 137 ms with map and sum
Sum of ages = 220000000 and it took : 134 ms with map and sum and parallel stream

我尝试了几次,每次都给我不同的结果(大多数时候最后一个解决方案是最好的),所以我想知道:

1)正确的做法是什么?

2)为什么? (与其他解决方案有什么区别?)

最佳答案

在我们进入实际答案之前,你应该知道一些事情:

  • 您的测试结果可能会有很大差异,这取决于许多因素(例如您运行它的计算机)。以下是在我的 8 核机器上运行一次的结果:
    Sum of ages = 260000000 and it took : 94 ms with collectors
    Sum of ages = 260000000 and it took : 61 ms with collectors and parallel stream
    Sum of ages = 260000000 and it took : 70 ms with map and sum
    Sum of ages = 260000000 and it took : 94 ms with map and sum and parallel stream
    

    然后在以后的运行中:
    Sum of ages = 260000000 and it took : 68 ms with collectors
    Sum of ages = 260000000 and it took : 67 ms with collectors and parallel stream
    Sum of ages = 260000000 and it took : 66 ms with map and sum
    Sum of ages = 260000000 and it took : 109 ms with map and sum and parallel stream
    
  • 微基准测试不是一个简单的话题。有一些方法可以做到(我稍后会介绍)但只是尝试使用 System.currentTimeMillies()在大多数情况下不会可靠地工作。
  • 仅仅因为 Java 8 使并行操作变得简单,这并不意味着它们应该在任何地方使用。并行操作在某些情况下有意义,而在其他情况下则不然。

  • 好的,现在让我们来看看您正在使用的各种方法。
  • 顺序收集器: summingInt您使用的收集器具有以下实现:
    public static <T> Collector<T, ?, Integer> summingInt(ToIntFunction<? super T> mapper) {
        return new CollectorImpl<>(
                () -> new int[1],
                (a, t) -> { a[0] += mapper.applyAsInt(t); },
                (a, b) -> { a[0] += b[0]; return a; },
                a -> a[0], Collections.emptySet());
    }
    

    因此,首先将创建一个包含一个元素的新数组。然后对于每个 Person流中的元素 collect函数将使用 Person#getAge()函数将年龄检索为 Integer (不是 int !)并将该年龄添加到以前的年龄(在一维数组中)。最后,当处理完整个流时,它将从该数组中提取值并返回它。所以,这里有很多自动装箱和拆箱。
  • 并行收集器:这使用 ReferencePipeline#forEach(Consumer)函数来累积它从映射函数中得到的年龄。同样有很多自动装箱和拆箱。
  • 顺序映射和求和:在这里您映射您的 Stream<Person>IntStream .这意味着一件事是不再需要自动装箱或取消装箱;在某些情况下,这可以节省大量时间。然后它使用以下实现对结果流求和:
    @Override
    public final int sum() {
        return reduce(0, Integer::sum);
    }
    
    reduce这里的函数将调用 ReduceOps#ReduceOp#evaluateSequential(PipelineHelper<T> helper, Spliterator<P_IN> spliterator) .
    这实质上将使用 Integer::sum对所有数字进行函数处理,从 0 和第一个数字开始,然后是第二个数字的结果,依此类推。
  • 并行映射和求和:事情变得有趣了。它使用相同的 sum()函数,但是在这种情况下,reduce 将调用 ReduceOps#ReduceOp#evaluateParallel(PipelineHelper<T> helper, Spliterator<P_IN> spliterator)而不是顺序选项。这将基本上使用分而治之的方法来将值相加。现在,分而治之的一大优势当然是它可以轻松地并行完成。然而,它确实需要多次拆分和重新加入流,这会花费时间。因此,它的速度可能会有很大差异,具体取决于与元素有关的实际任务的复杂性。在添加的情况下,大多数情况下可能不值得;从我的结果中可以看出,它始终是较慢的方法之一。

  • 现在,要真正了解需要多长时间,让我们做一个适当的微基准测试。我将使用 JMH使用以下基准代码:
    package com.stackoverflow.user2352924;
    
    import org.openjdk.jmh.annotations.*;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.concurrent.TimeUnit;
    import java.util.stream.Collectors;
    
    @BenchmarkMode(Mode.Throughput)
    @OutputTimeUnit(TimeUnit.MINUTES)
    @Warmup(iterations = 5, time = 5, timeUnit = TimeUnit.SECONDS)
    @Measurement(iterations = 10, time = 10, timeUnit = TimeUnit.SECONDS)
    @State(Scope.Benchmark)
    @Fork(1)
    @Threads(2)
    public class MicroBenchmark {
    
        private static List<Person> persons = new ArrayList<>();
    
        private int test;
    
        static {
            for(int i=0; i < 10000000; i++){
                persons.add(new Person("random", 26));
            }
        }
    
        @Benchmark
        public void sequentialCollectors() {
            test = 0;
            test += persons.stream().collect(Collectors.summingInt(p -> p.getAge()));
        }
    
        @Benchmark
        public void parallelCollectors() {
            test = 0;
            test += persons.parallelStream().collect(Collectors.summingInt(p -> p.getAge()));
        }
    
        @Benchmark
        public void sequentialMapSum() {
            test = 0;
            test += persons.stream().mapToInt(p -> p.getAge()).sum();
        }
    
        @Benchmark
        public void parallelMapSum() {
            test = 0;
            test += persons.parallelStream().mapToInt(p -> p.getAge()).sum();
        }
    
    }
    
    pom.xml对于这个 Maven 项目,它看起来像这样:
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.stackoverflow.user2352924</groupId>
        <artifactId>StackOverflow</artifactId>
        <version>1.0</version>
        <packaging>jar</packaging>
    
        <name>Auto-generated JMH benchmark</name>
    
        <prerequisites>
            <maven>3.0</maven>
        </prerequisites>
    
        <dependencies>
            <dependency>
                <groupId>org.openjdk.jmh</groupId>
                <artifactId>jmh-core</artifactId>
                <version>${jmh.version}</version>
            </dependency>
            <dependency>
                <groupId>org.openjdk.jmh</groupId>
                <artifactId>jmh-generator-annprocess</artifactId>
                <version>${jmh.version}</version>
                <scope>provided</scope>
            </dependency>
        </dependencies>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <jmh.version>0.9.5</jmh.version>
            <javac.target>1.8</javac.target>
            <uberjar.name>benchmarks</uberjar.name>
        </properties>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.1</version>
                    <configuration>
                        <compilerVersion>${javac.target}</compilerVersion>
                        <source>${javac.target}</source>
                        <target>${javac.target}</target>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                    <version>2.2</version>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>shade</goal>
                            </goals>
                            <configuration>
                                <finalName>microbenchmarks</finalName>
                                <transformers>
                                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                        <mainClass>org.openjdk.jmh.Main</mainClass>
                                    </transformer>
                                </transformers>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
            <pluginManagement>
                <plugins>
                    <plugin>
                        <artifactId>maven-clean-plugin</artifactId>
                        <version>2.5</version>
                    </plugin>
                    <plugin>
                        <artifactId>maven-deploy-plugin</artifactId>
                        <version>2.8.1</version>
                    </plugin>
                    <plugin>
                        <artifactId>maven-install-plugin</artifactId>
                        <version>2.5.1</version>
                    </plugin>
                    <plugin>
                        <artifactId>maven-jar-plugin</artifactId>
                        <version>2.4</version>
                    </plugin>
                    <plugin>
                        <artifactId>maven-javadoc-plugin</artifactId>
                        <version>2.9.1</version>
                    </plugin>
                    <plugin>
                        <artifactId>maven-resources-plugin</artifactId>
                        <version>2.6</version>
                    </plugin>
                    <plugin>
                        <artifactId>maven-site-plugin</artifactId>
                        <version>3.3</version>
                    </plugin>
                    <plugin>
                        <artifactId>maven-source-plugin</artifactId>
                        <version>2.2.1</version>
                    </plugin>
                    <plugin>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <version>2.17</version>
                    </plugin>
                </plugins>
            </pluginManagement>
        </build>
    
    </project>
    

    确保 Maven 也与 Java 8 一起运行,否则你会得到丑陋的错误。

    我不会在这里详细介绍如何使用 JMH(还有其他地方可以这样做),但这是我得到的结果:
    # Run complete. Total time: 00:08:48
    
    Benchmark                                     Mode  Samples     Score  Score error    Units
    c.s.u.MicroBenchmark.parallelCollectors      thrpt       10  3658,949      775,115  ops/min
    c.s.u.MicroBenchmark.parallelMapSum          thrpt       10  2616,905      221,109  ops/min
    c.s.u.MicroBenchmark.sequentialCollectors    thrpt       10  5502,160      439,024  ops/min
    c.s.u.MicroBenchmark.sequentialMapSum        thrpt       10  6120,162      609,232  ops/min
    

    因此,在我运行这些测试时的系统上,顺序映射求和要快得多,在并行映射求和(使用分治法)仅能完成 2600 . 事实上,顺序方法都比并行方法快得多。

    现在,在更容易并行运行的情况下 - 例如哪里Person#getAge() function 比 getter 复杂得多 - 并行方法很可能是更好的解决方案。归根结底,这一切都取决于在被测试的情况下并行运行的效率。

    要记住的另一件事:如果有疑问,请进行适当的微基准测试。 ;-)

    关于java - 我应该如何用流来总结一些东西?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24198561/

    有关java - 我应该如何用流来总结一些东西?的更多相关文章

    1. ruby - 难道Lua没有和Ruby的method_missing相媲美的东西吗? - 2

      我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/

    2. ruby - 检查 "command"的输出应该包含 NilClass 的意外崩溃 - 2

      为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar

    3. java - 等价于 Java 中的 Ruby Hash - 2

      我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/

    4. java - 从 JRuby 调用 Java 类的问题 - 2

      我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www

    5. ruby-on-rails - 如何生成传递一些自定义参数的 `link_to` URL? - 2

      我正在使用RubyonRails3.0.9,我想生成一个传递一些自定义参数的link_toURL。也就是说,有一个articles_path(www.my_web_site_name.com/articles)我想生成如下内容:link_to'Samplelinktitle',...#HereIshouldimplementthecode#=>'http://www.my_web_site_name.com/articles?param1=value1¶m2=value2&...我如何编写link_to语句“alàRubyonRailsWay”以实现该目的?如果我想通过传递一些

    6. java - 我的模型类或其他类中应该有逻辑吗 - 2

      我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我

    7. java - 什么相当于 ruby​​ 的 rack 或 python 的 Java wsgi? - 2

      什么是ruby​​的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht

    8. Observability:从零开始创建 Java 微服务并监控它 (二) - 2

      这篇文章是继上一篇文章“Observability:从零开始创建Java微服务并监控它(一)”的续篇。在上一篇文章中,我们讲述了如何创建一个Javaweb应用,并使用Filebeat来收集应用所生成的日志。在今天的文章中,我来详述如何收集应用的指标,使用APM来监控应用并监督web服务的在线情况。源码可以在地址 https://github.com/liu-xiao-guo/java_observability 进行下载。摄入指标指标被视为可以随时更改的时间点值。当前请求的数量可以改变任何毫秒。你可能有1000个请求的峰值,然后一切都回到一个请求。这也意味着这些指标可能不准确,你还想提取最小/

    9. 【Java 面试合集】HashMap中为什么引入红黑树,而不是AVL树呢 - 2

      HashMap中为什么引入红黑树,而不是AVL树呢1.概述开始学习这个知识点之前我们需要知道,在JDK1.8以及之前,针对HashMap有什么不同。JDK1.7的时候,HashMap的底层实现是数组+链表JDK1.8的时候,HashMap的底层实现是数组+链表+红黑树我们要思考一个问题,为什么要从链表转为红黑树呢。首先先让我们了解下链表有什么不好???2.链表上述的截图其实就是链表的结构,我们来看下链表的增删改查的时间复杂度增:因为链表不是线性结构,所以每次添加的时候,只需要移动一个节点,所以可以理解为复杂度是N(1)删:算法时间复杂度跟增保持一致查:既然是非线性结构,所以查询某一个节点的时候

    10. SPI接收数据异常问题总结 - 2

      SPI接收数据左移一位问题目录SPI接收数据左移一位问题一、问题描述二、问题分析三、探究原理四、经验总结最近在工作在学习调试SPI的过程中遇到一个问题——接收数据整体向左移了一位(1bit)。SPI数据收发是数据交换,因此接收数据时从第二个字节开始才是有效数据,也就是数据整体向右移一个字节(1byte)。请教前辈之后也没有得到解决,通过在网上查阅前人经验终于解决问题,所以写一个避坑经验总结。实际背景:MCU与一款芯片使用spi通信,MCU作为主机,芯片作为从机。这款芯片采用的是它规定的六线SPI,多了两根线:RDY和INT,这样从机就可以主动请求主机给主机发送数据了。一、问题描述根据从机芯片手

    随机推荐