MCPcopy Index your code
hub / github.com/BruceEckel/OnJava8-Examples / main

Method main

concurrent/Summing.java:26–40  ·  view source on GitHub ↗
(String[] args)

Source from the content-addressed store, hash-verified

24 public static final long CHECK =
25 (long)SZ * ((long)SZ + 1)/2; // Gauss's formula
26 public static void main(String[] args) {
27 System.out.println(CHECK);
28 timeTest("Sum Stream", CHECK, () ->
29 LongStream.rangeClosed(0, SZ).sum());
30 timeTest("Sum Stream Parallel", CHECK, () ->
31 LongStream.rangeClosed(0, SZ).parallel().sum());
32 timeTest("Sum Iterated", CHECK, () ->
33 LongStream.iterate(0, i -> i + 1)
34 .limit(SZ+1).sum());
35 // Slower & runs out of memory above 1_000_000:
36 // timeTest("Sum Iterated Parallel", CHECK, () ->
37 // LongStream.iterate(0, i -> i + 1)
38 // .parallel()
39 // .limit(SZ+1).sum());
40 }
41}
42/* Output:
435000000050000000

Callers

nothing calls this directly

Calls 2

timeTestMethod · 0.95
sumMethod · 0.80

Tested by

no test coverage detected