Question: In Fig. 17.24 , we implemented a stream pipeline that rolled a die 60,000,000 times using values produced by SecureRandom method ints. Use the same

In Fig. 17.24 , we implemented a stream pipeline that rolled a die 60,000,000 times using values produced by SecureRandom method ints.

Use the same timing techniques you used in Exercise 17.25 to time the original stream pipeline’s operation, then perform and time the operation using a parallel stream. Any improvement?

Exercise 17.25

In Fig. 17.24, we implemented a stream pipeline that rolled a die 60,000,000 times using values produced by SecureRandom method ints. Package java.time contains types Instant and Duration that you can use to capture the time before and after evaluating the stream pipeline, then calculate the difference between the Instants to determine the total time. Use Instant’s static method now to get the current time. To determine the difference between two Instants, use class Duration’s static method between, which returns a Duration object containing the time difference. Duration provides methods like toMillis to return a duration in milliseconds. Use these timing techniques to time the original stream pipeline’s operation, then do so again using class Random from package java.util, rather than SecureRandom.

Fig. 17.24

// Fig. 17.24: RandomIntStream.java // Rolling a die 60,000,000 times with streams 3 import

// Fig. 17.24: RandomIntStream.java // Rolling a die 60,000,000 times with streams 3 import java.security.Secure Random; 4 import java.util. function. Function; 5 import java.util.stream.Collectors; 6 127 7 8 10 11 12 13 14 15 16 17 18 19 20 public class Random IntStream { public static void main(String[] args) { SecureRandom random = new SecureRandom(); 123456 } } // roll a die 60,000,000 times and summarize the results System.out.printf("%-6s%s %n", "Face", "Frequency"); random.ints (60_000_000, 1, 7) .boxed () Face Frequency 9992993 10000363 10002272 10003810 10000321 10000241 .collect(Collectors.grouping By (Function.identity (), Collectors.counting())) .forEach((face, frequency) -> System.out.printf("%-6d%d %n", face, frequency));

Step by Step Solution

3.42 Rating (155 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Based on the provided information and the image of Fig 1724 I will guide you through how to time the stream pipelines operation for both a sequential ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Java How To Program Late Objects Questions!