In this lecture, we are going to see the difference between sequential and parallel streams.
So, for example, we would like to measure the time.
So that's why we get along start, which is the system that current time amylase, then I would like
to create a long see, which is an integer stream.
So javadoc util that stream, that integer stream.
And I would like to call the range close method that returns a sequential order integer stream from
start inquisitive to and inclusive by an incremental step of one.
So starting with zero up to one billion then I would like to filter and I is the I modulo two is equal
to zero.
What does it mean that I would like to filter out the even numbers.
So this is the so-called modulo operator, which means that if we divide the given integer by two,
then the remainder is equal to zero, which means that the given number is even and I would like to
count the number of even the values.
Okay.
And this is the operation we are going to measure.
So we have the duration, which is system that current time millions minus the start.
And then I would like to printout counted the C variable and in plus the duration milliseconds.
So if we use a sequential stream, if we run it, then it is going to take a while to count the number
of even values.
One thousand five hundred milliseconds.
What if we would like to use parallelization?
Then we can call it the parallel method and then we would like to call the filter.
So with the help of this parallel method, it returns an equivalent stream that is parallel.
So let's try to memorize that sequential result was one thousand five hundred milliseconds.
What about the Pedaler solution?
As you can see, it is approximately five hundred milliseconds.
So what does it mean that using parallelization can be extremely powerful if the data set is huge?
In this case, we have a billion values, so we have a huge dataset we want to filter.
So it is definitely a good idea to use parallelization.
But if we have, for example, just ten items, then using parallelization is not going to be faster
than the sequential approach.
In this case, it is fifty milliseconds and we do not use parallelization then I guess it is going to
be faster.
So as you can see, using sequential approach is faster than using parallelization.
If we rerun the application, it may be even faster.
OK, so this is the crucial difference between sequential streams and parallel streams.
With the help of parallelization, we can take advantage of multiple processors or multiple processor
cores.
Banks are watching.