WEBVTT 00:01.690 --> 00:07.390 Welcome in this lecture, I'm going to talk about Vanderbeke, incorrect of a slice, Groff's in much 00:07.390 --> 00:08.020 more detail. 00:08.230 --> 00:09.340 All right, let's get started. 00:11.630 --> 00:17.060 By the way, to understand this lecture fully, you may want to check out the previous retro led class 00:17.080 --> 00:21.620 section, because in this lecture I'm going to use a few functions that I've already explained there 00:21.620 --> 00:22.010 before. 00:22.020 --> 00:23.760 I'm going to eliminate some of the things here. 00:24.400 --> 00:27.220 OK, let me first impart my predecessor's package. 00:27.230 --> 00:33.020 I'm going to set some options for printing the begging and printing the slides nicely to the screen. 00:33.560 --> 00:34.820 You may change this, by the way. 00:34.820 --> 00:35.750 They are not important. 00:36.380 --> 00:36.740 All right. 00:36.980 --> 00:42.990 First of all, let me show you how the array of a slice grows using a simple animation to do that. 00:43.010 --> 00:45.830 I'm going to declare a new slice here. 00:45.830 --> 00:51.680 I'm going to clear the screen first and a loop that will loop until the capacity of the slice becomes 00:51.680 --> 00:53.570 greater than one hundred twenty eight. 00:53.690 --> 00:55.760 This number is not important, by the way. 00:55.760 --> 00:57.230 You can set it to anything you like. 00:57.590 --> 01:02.840 OK, here, I'm going to move the cursor to the top, then I'm going to print the num slice here. 01:04.500 --> 01:08.910 And I'm going to appoint a random integer between one and nine to the number like so. 01:10.430 --> 01:14.390 And lastly, I'm going to sleep quarter of a second before the next loop step. 01:15.490 --> 01:16.250 All right, ready? 01:16.600 --> 01:17.590 Let's run it. 01:18.130 --> 01:23.560 It starts with a slice without an array first, so all the fields in the slicer there are zero. 01:24.100 --> 01:27.910 Now, I want you to pay attention to the fields of the numb slice here. 01:28.330 --> 01:34.300 You will see that whenever the length and the capacity become equal, the apparent function will allocate 01:34.300 --> 01:34.870 a new worry. 01:35.170 --> 01:42.040 But it won't do so when there is enough capacity in the back injury and whenever the slices a new back, 01:42.550 --> 01:46.120 its pointer will change to refer to the new array. 01:46.720 --> 01:48.190 All right, let's get started. 01:48.520 --> 01:51.300 It starts with a slice that doesn't have a back injury. 01:51.520 --> 01:53.890 So the first opponent creates a new back injury. 01:54.250 --> 01:56.850 Now, the slice has a capacity for only one element. 01:57.310 --> 02:03.910 So the next part creates another array with a capacity off to the torso and allocates Anura again with 02:03.910 --> 02:06.710 a capacity of for the capacity is not full yet. 02:06.730 --> 02:09.140 So the next opponent doesn't create a new era. 02:09.310 --> 02:13.780 However, expanding the fifth element creates a new era with a capacity of eight. 02:13.930 --> 02:19.810 The length is less than the capacity, so the slice will continue using the same array for three additional 02:19.810 --> 02:20.260 elements. 02:20.440 --> 02:26.510 Now the back injury is full again, so the next opponent creates a new era with a capacity of 16. 02:26.650 --> 02:32.650 So the numskulls can use the same array for seven additional elements without creating a new beginning 02:32.650 --> 02:32.980 or a. 02:34.590 --> 02:40.980 Now the capacity is full again, so the next aband creates a larger array with a capacity of 32 and 02:40.980 --> 02:45.330 it goes like this, I feel like kind of support commentator right now. 02:47.060 --> 02:47.550 OK. 02:47.600 --> 02:53.090 And after several calls, their plans of the couple's these six to four instead of 32, as you can see, 02:53.240 --> 02:58.370 the apan function doubles the capacity of the slides whenever it needs to allocate a larger array. 02:58.400 --> 03:04.310 Remember, creating something on the memory is a costly operation so that a path function tries to prevent 03:04.310 --> 03:07.690 creating a new worry each time you apart to a slice. 03:08.300 --> 03:11.600 Let me show you this one more time without my commentary. 03:23.250 --> 03:27.270 Now, let's take a look at how the cup is, the changes for larger slices. 03:27.870 --> 03:29.790 Remove all the court here first. 03:30.090 --> 03:36.930 OK, now I'm going to create and insulin's only with one element and a variable for storing the previous 03:36.930 --> 03:38.040 capacity like so. 03:39.100 --> 03:44.080 By the way, I'm using a flawed for the old cap variable because I'm going to calculate the Girlfight 03:44.080 --> 03:44.630 ratio. 03:44.980 --> 03:50.170 OK, here I'm going to loop until the length of the slice becomes less than half a million. 03:50.950 --> 03:54.000 Now, I'm going to appoint a dummy element to the age of slice. 03:55.140 --> 04:01.010 OK, here, I'm going to save the capacity of the age, a slice to a new variable to compare with afterward 04:01.740 --> 04:05.550 here I'm going to print a message when the capacity of the slides changes. 04:06.700 --> 04:13.690 Of course, doing so is an error because the old cap is a flawed 64, but see, variable is an int is 04:13.690 --> 04:17.270 because the function returns and intact land function. 04:18.010 --> 04:21.790 So let me convert the C variable to a float to four here. 04:22.220 --> 04:28.840 OK, after this, I'm going to update the previous capacity to the current capacity laakso so that I 04:28.840 --> 04:33.910 will be able to compare the previous capacity to the current capacity in this if statement. 04:34.150 --> 04:39.190 All right, now it's time to print the message whenever the slices backing are across this print, the 04:39.190 --> 04:42.460 length capacity and the Girlfight ratio ALAC. 04:42.460 --> 04:49.840 So now I need to pass the current lengths of the slice, its current capacity and the growth ratio by 04:49.840 --> 04:53.330 dividing the current capacity to the previous capacity lachsa. 04:53.500 --> 05:00.040 So this loop pants and element to the eight slice half a million times and each time the slices captures 05:00.040 --> 05:05.560 the changes, it brings a message with a Girlfight ratio compared to the slices previous capacity. 05:05.590 --> 05:06.730 All right, let's try it. 05:07.890 --> 05:14.070 As you can see, the apan function doubles the capacity of the begging increase until the area has exactly 05:14.070 --> 05:20.130 one thousand twenty four elements, and after that threshold and function starts to grow, the area 05:20.130 --> 05:21.270 is a little bit slower. 05:21.960 --> 05:27.780 If you take a look at this part, you can see that the growth ratio is much slower, around 25 percent. 05:28.020 --> 05:33.370 This happens because growing a larger area is much more costly than growing a smaller area. 05:33.930 --> 05:39.450 Think about creating an array with hundreds, thousands of elements and copying the preview say it's 05:39.600 --> 05:44.970 50 thousands of elements to it so that palm function slows down the growth ratio of Anamur. 05:44.970 --> 05:46.670 The Bettinger gets large enough. 05:46.680 --> 05:51.750 It assumes that you're not going to appoint a lot of elements onto the larger array groves again. 05:52.110 --> 05:55.300 So in summary, you can absolutely trust the app and function. 05:55.680 --> 05:58.020 Don't try to optimize it unless necessary. 05:58.410 --> 06:00.720 Most of the time you can work with it directly. 06:01.170 --> 06:03.240 Don't worry about the array allocations. 06:03.240 --> 06:06.720 If everything works fine, do not prematurely optimize. 06:07.990 --> 06:13.570 In the next lecture, I'll show you how to optimize the apan function for larger slices using the make 06:13.570 --> 06:14.000 function. 06:14.560 --> 06:15.160 All right. 06:15.190 --> 06:15.950 That's all for now. 06:16.060 --> 06:17.440 See in the next lecture by.