Java Reference
In-Depth Information
When benchmarking the code in Examples 6-1 and 6-2 on a 4-core machine with 10 albums,
the sequential code was 8× faster. Upon expanding the number of albums to 100, they were
both equally fast, and by the time we hit 10,000 albums, the parallel code was 2.5× faster.
NOTE
Any specific benchmark figures in this chapter are listed only to make a point. If you try
to replicate these results on your hardware, you may get drastically different outcomes.
The size of the input stream isn't the only factor to think about when deciding whether
there's a parallel speedup. It's possible to get varying performance numbers based upon how
you wrote your code and how many cores are available. We'll look at this in a bit more detail
in Performance , but first let's look at a more complex example.
Simulations
The kinds of problems that parallel stream libraries excel at are those that involve simple op-
erations processing a lot of data, such as simulations. In this section, we'll be building a
simple simulation to understand dice throws, but the same ideas and approach can be used on
larger and more realistic problems.
The kind of simulation we'll be looking at here is a Monte Carlo simulation. Monte Carlo
simulations work by running the same simulation many times over with different random
seeds on every run. The results of each run are recorded and aggregated in order to build up a
comprehensive simulation. They have many uses in engineering, finance, and scientific com-
puting.
If we throw a fair die twice and add up the number of dots on the winning side, we'll get a
number between 2 and 12. This must be at least 2 because the fewest number of dots on each
side is 1 and there are two dice. The maximum score is 12, as the highest number you can
score on each die is 6. We want to try and figure out what the probability of each number
between 2 and 12 is.
One approach to solving this problem is to add up all the different combinations of dice rolls
that can get us each value. For example, the only way we can get 2 is by rolling 1 and then 1
again. There are 36 different possible combinations, so the probability of the two sides
adding up to 2 is 1 in 36, or 1/36.
Search WWH ::




Custom Search