Hi everyone.
Welcome back to this tutorial.
In this tutorial, we will explore the performance of these two methods.
I missed one thing here.
We take a look at this.
In this method I'm not calling the parallel method.
I'm going to make a call to this parallel method and then run this and then check whether the result
are the same.
We take a look at it.
The result are the same.
Now, in this tutorial we are going to focus on calculating the performance.
Right?
So for this purpose, what I'm going to do, I'm going to create a method called.
Public.
Static and then it is going to return a long it is going to be check performance result.
And I would like to call these methods multiple times.
So I want that to be a parameter to this method number of times.
And then this is going to be so let's construct the for loop int I equal to zero I less than number
of times then I plus plus.
So in here we are going to call the some sequential stream and some parallel stream.
And I would like to change this one to camel casing because that is a recommendation for the method
names in Java.
So I'm clicking refactor and then selecting the rename.
Some sequential.
Think, right, what is good.
And then this is going to be some parallel string refactor rename.
Some parallel string.
There we go.
All set in here.
We are going to call some sequential stream and some parallel stream.
Right.
So I don't want to make the call to these two methods inside the for loop.
I want to make the call separately and then take the result how it is performing.
How can we do that?
So for this method, right now I'm just passing the INT number of times I want to pass this method itself
as a parameter to it.
How can we achieve it?
We take a look at this example.
This looks like a supplier, if you remember from the previous tutorials, Supplier is a functional
interface which doesn't accept any input, but it is going to return you a output.
This particular method, these two methods look exactly like a.
The player.
So what I'm going to do in the place of this, I'm going to call, check performance result, and then
I'm going to do parallel stream example.
I'm going to make use of the method reference syntax.
So as soon as I gave that to Colon is giving me the option, I'm going to call the sum sequential stream
and then I'm going to make the call 20 times.
So it is complaining.
What is that?
It is complaining.
The parallel stream right now has only one method.
Right?
I'm going to change this one to supplier and of type integer because the return type is going to be
an integer supplier and all those functional interfaces always refer to the wrapper version of the primitive
type.
So that's the reason why we have to give integer instead of int.
I'm going to name this one as supplier.
Let's import this.
There you go.
So and then the next one, what is the next one?
The next one is going to be some parallel string, right?
Let me give some code comments here is going to be sequential stream result.
Can put this whole thing down and this is going to be a parallel stream result.
So we take a look at it.
This one is calling the sequential stream, and this one is calling the parallel stream.
So in here, what we have to do, we have to call supplier dot get.
So this is going to actually invoke these two methods, right?
The next step is we want to calculate how long did it take?
I'm going to use the system class for this purpose.
It's going to be start time system dot current time milliseconds.
And end time is going to be system dot runtime milliseconds and the return is going to be in time minus
the start time.
There you go.
So this is going to give you the total time it took to execute this method, the number of times, the
number of times value that we are passing is that 20 times.
So this some sequential stream is going to be called 20 times and some parallel stream is going to be
called 20 times.
And then this is going to give you the number of.
Then how many milliseconds that it took in order to calculate the output for you.
Right.
I'm going to run this example.
We run this example.
It took like this one.
The sequential stream result took 15 milliseconds, but the parallel stream just took seven milliseconds.
So what happens behind the scenes?
What happens under the hood?
How many threads got created?
Those are the interesting things which might be running in your mind.
I'm going to cover those in the next tutorial.
With this, we came to the end of this tutorial.
Thank you for watching.