Welcome in this lecture, I'm going to talk about Vanderbeke, incorrect of a slice, Groff's in much
more detail.
All right, let's get started.
By the way, to understand this lecture fully, you may want to check out the previous retro led class
section, because in this lecture I'm going to use a few functions that I've already explained there
before.
I'm going to eliminate some of the things here.
OK, let me first impart my predecessor's package.
I'm going to set some options for printing the begging and printing the slides nicely to the screen.
You may change this, by the way.
They are not important.
All right.
First of all, let me show you how the array of a slice grows using a simple animation to do that.
I'm going to declare a new slice here.
I'm going to clear the screen first and a loop that will loop until the capacity of the slice becomes
greater than one hundred twenty eight.
This number is not important, by the way.
You can set it to anything you like.
OK, here, I'm going to move the cursor to the top, then I'm going to print the num slice here.
And I'm going to appoint a random integer between one and nine to the number like so.
And lastly, I'm going to sleep quarter of a second before the next loop step.
All right, ready?
Let's run it.
It starts with a slice without an array first, so all the fields in the slicer there are zero.
Now, I want you to pay attention to the fields of the numb slice here.
You will see that whenever the length and the capacity become equal, the apparent function will allocate
a new worry.
But it won't do so when there is enough capacity in the back injury and whenever the slices a new back,
its pointer will change to refer to the new array.
All right, let's get started.
It starts with a slice that doesn't have a back injury.
So the first opponent creates a new back injury.
Now, the slice has a capacity for only one element.
So the next part creates another array with a capacity off to the torso and allocates Anura again with
a capacity of for the capacity is not full yet.
So the next opponent doesn't create a new era.
However, expanding the fifth element creates a new era with a capacity of eight.
The length is less than the capacity, so the slice will continue using the same array for three additional
elements.
Now the back injury is full again, so the next opponent creates a new era with a capacity of 16.
So the numskulls can use the same array for seven additional elements without creating a new beginning
or a.
Now the capacity is full again, so the next aband creates a larger array with a capacity of 32 and
it goes like this, I feel like kind of support commentator right now.
OK.
And after several calls, their plans of the couple's these six to four instead of 32, as you can see,
the apan function doubles the capacity of the slides whenever it needs to allocate a larger array.
Remember, creating something on the memory is a costly operation so that a path function tries to prevent
creating a new worry each time you apart to a slice.
Let me show you this one more time without my commentary.
Now, let's take a look at how the cup is, the changes for larger slices.
Remove all the court here first.
OK, now I'm going to create and insulin's only with one element and a variable for storing the previous
capacity like so.
By the way, I'm using a flawed for the old cap variable because I'm going to calculate the Girlfight
ratio.
OK, here I'm going to loop until the length of the slice becomes less than half a million.
Now, I'm going to appoint a dummy element to the age of slice.
OK, here, I'm going to save the capacity of the age, a slice to a new variable to compare with afterward
here I'm going to print a message when the capacity of the slides changes.
Of course, doing so is an error because the old cap is a flawed 64, but see, variable is an int is
because the function returns and intact land function.
So let me convert the C variable to a float to four here.
OK, after this, I'm going to update the previous capacity to the current capacity laakso so that I
will be able to compare the previous capacity to the current capacity in this if statement.
All right, now it's time to print the message whenever the slices backing are across this print, the
length capacity and the Girlfight ratio ALAC.
So now I need to pass the current lengths of the slice, its current capacity and the growth ratio by
dividing the current capacity to the previous capacity lachsa.
So this loop pants and element to the eight slice half a million times and each time the slices captures
the changes, it brings a message with a Girlfight ratio compared to the slices previous capacity.
All right, let's try it.
As you can see, the apan function doubles the capacity of the begging increase until the area has exactly
one thousand twenty four elements, and after that threshold and function starts to grow, the area
is a little bit slower.
If you take a look at this part, you can see that the growth ratio is much slower, around 25 percent.
This happens because growing a larger area is much more costly than growing a smaller area.
Think about creating an array with hundreds, thousands of elements and copying the preview say it's
50 thousands of elements to it so that palm function slows down the growth ratio of Anamur.
The Bettinger gets large enough.
It assumes that you're not going to appoint a lot of elements onto the larger array groves again.
So in summary, you can absolutely trust the app and function.
Don't try to optimize it unless necessary.
Most of the time you can work with it directly.
Don't worry about the array allocations.
If everything works fine, do not prematurely optimize.
In the next lecture, I'll show you how to optimize the apan function for larger slices using the make
function.
All right.
That's all for now.
See in the next lecture by.