栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在mapToInt之后调用map会有什么好处,如果需要的话

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在mapToInt之后调用map会有什么好处,如果需要的话

如有疑问,请测试!使用jmh,我在100k元素的列表上得到以下结果(以微秒为单位,更好):

Benchmark  Mode  Samples     Score    Error  Unitsc.a.p.SO32462798.for_loop        avgt       10   119.110    0.921  us/opc.a.p.SO32462798.mapToInt        avgt       10   129.702    1.040  us/opc.a.p.SO32462798.mapToInt_map    avgt       10   129.753    1.516  us/opc.a.p.SO32462798.map_reduce      avgt       10  1262.802   12.197  us/opc.a.p.SO32462798.summingInt      avgt       10   134.821    1.203  us/op

因此,从快到慢,您已经拥有:

  • for(int i : list) sum += i*i;
  • mapToInt(x -> x * x).sum()
    mapToInt(x -> x).map(x -> x * x).sum()
  • collect(Collectors.summingInt(x -> x * x))
  • map(x -> x * x).reduce((x, y) -> x + y).get()

请注意,结果在很大程度上取决于JIT优化。如果映射中的逻辑更加复杂,则某些优化可能不可用(较长的代码=较少的内联),在这种情况下,流版本可能比for循环花费4-5倍的时间-
但是,如果该逻辑占用大量CPU资源,差异将再次减小。对您的实际应用程序进行性能分析将为您提供更多信息。


基准代码供参考:

@State(Scope.Benchmark)@BenchmarkMode(Mode.AverageTime)public class SO32462798 {  List<Integer> list;  @Setup public void setup() {    list = new Random().ints(100_000).boxed().collect(toList());  }  @Benchmark public int for_loop() {    int sum = 0;    for (int i : list) sum += i * i;    return sum;  }  @Benchmark public int summingInt() {    return list.stream().collect(Collectors.summingInt(x -> x * x));  }  @Benchmark public int mapToInt() {    return list.stream().mapToInt(x -> x * x).sum();  }  @Benchmark public int mapToInt_map() {    return list.stream().mapToInt(x -> x).map(x -> x * x).sum();  }  @Benchmark public int map_reduce() {    return list.stream().map(x -> x * x).reduce((x, y) -> x + y).get();  }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/508772.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号