DoubleSummaryStatistics

Simply put

DoubleSummaryStatistics is a class used for collecting statistical information about a set of double values. It provides a convenient way to calculate the minimum, maximum, sum, average, and count of a series of double values.

Internally, DoubleSummaryStatistics uses several properties to track the collected statistical information:

  • count: the number of values
  • sum: the sum of all values
  • min: the minimum value
  • max: the maximum value

When we use DoubleSummaryStatistics to collect statistics on a series of double values, it iterates over each value, updating these properties by comparing the values.

In Java, we often use the Collectors.summarizingDouble collector to convert a stream of double values into a DoubleSummaryStatistics object. During this process, the collector traverses each value in the stream, updating the properties of the DoubleSummaryStatistics object.

When dealing with a large number of double values, DoubleSummaryStatistics may internally use optimized algorithms to efficiently calculate the statistical information, minimizing the number of traversal and value comparison operations.


Benchmark

import java.util.ArrayList;
import java.util.DoubleSummaryStatistics;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

public class DoubleSummaryStatisticsBenchmark {

    public static void main(String[] args) {
        int size = 100000000; // adjust size for your needs

        // Generate random double list
        List<Double> list = new ArrayList<>(size);
        Random random = new Random();
        for (int i = 0; i < size; i++) {
            list.add(random.nextDouble());
        }

        // Benchmark using traditional loop
        long startTimeLoop = System.nanoTime();
        double minLoop = Double.MAX_VALUE;
        double maxLoop = Double.MIN_VALUE;
        double sumLoop = 0;
        long countLoop = 0;
        for (double value : list) {
            minLoop = Math.min(minLoop, value);
            maxLoop = Math.max(maxLoop, value);
            sumLoop += value;
            countLoop++;
        }
        long endTimeLoop = System.nanoTime();
        double timeLoop = (endTimeLoop - startTimeLoop) / 1e9;

        // Benchmark using DoubleSummaryStatistics
        long startTimeStats = System.nanoTime();
        DoubleSummaryStatistics stats = list.stream().collect(Collectors.summarizingDouble(Double::doubleValue));
        long endTimeStats = System.nanoTime();
        double timeStats = (endTimeStats - startTimeStats) / 1e9;

        // Print results
        System.out.println("Size: " + size);
        System.out.println("Loop Time (s): " + timeLoop);
        System.out.println("Stream Time (s): " + timeStats);
        System.out.println("Min (Loop): " + minLoop);
        System.out.println("Max (Loop): " + maxLoop);
        System.out.println("Sum (Loop): " + sumLoop);
        System.out.println("Count (Loop): " + countLoop);
        System.out.println("Min (Stream): " + stats.getMin());
        System.out.println("Max (Stream): " + stats.getMax());
        System.out.println("Sum (Stream): " + stats.getSum());
        System.out.println("Count (Stream): " + stats.getCount());
    }
}

See

https://github.com/mtopolnik/billion-row-challenge/blob/main/src/Blog1.java

DoubleSummaryStatistics getMax() method in Java with Examples - GeeksforGeeks

  • 7
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

P("Struggler") ?

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值