WEBVTT

00:00.260 --> 00:06.500
Okay guys, now finally with references let's have an example with loops.

00:08.150 --> 00:12.080
So this would be a for each loop ref.

00:12.080 --> 00:14.090
So let me create such file.

00:14.540 --> 00:18.260
So I just wanted without any preconditions.

00:18.260 --> 00:24.110
Show you an example where we have a large data set generated but a large data set.

00:24.110 --> 00:29.150
And we're going to see how can we access data by reference.

00:29.150 --> 00:34.580
And we're going to see if there was any difference which version was faster.

00:34.580 --> 00:38.810
It might also be useful for you to do such experiments.

00:39.890 --> 00:44.750
So to this large array variable I'm going to assign the result of range function.

00:44.750 --> 00:50.840
So range function allows us to generate a big array with some value.

00:50.870 --> 00:53.000
Let's just use the default one.

00:53.420 --> 00:57.200
And I'd like 1 million elements.

00:57.200 --> 01:05.030
By the way, in PHP you can write numbers like this using underscore just for it to be more visually

01:05.030 --> 01:05.750
readable.

01:05.780 --> 01:09.230
This is still a valid number 1 million.

01:10.160 --> 01:14.240
Now let's see how can we calculate the time of the execution.

01:14.240 --> 01:20.510
So we can use the Microtime function and pass true to it.

01:21.110 --> 01:24.740
So we're gonna get the time at specific points.

01:24.890 --> 01:27.170
So this is before we run the code.

01:27.170 --> 01:30.020
And then we're going to do the same after we run the code.

01:30.020 --> 01:32.780
And we're gonna show the difference.

01:33.710 --> 01:33.950
Okay.

01:33.980 --> 01:36.470
So what are we about to do?

01:36.590 --> 01:43.220
Well I would like to just double every element inside this large array.

01:43.220 --> 01:46.100
Keep in mind it's 1 million elements.

01:46.100 --> 01:48.320
And I just want the result.

01:48.320 --> 01:52.190
So I just want the result to be stored someplace.

01:52.190 --> 01:58.700
So if we use the for each array well we need a kind of an output array.

01:58.790 --> 02:01.250
So let's define the out variable.

02:01.280 --> 02:05.360
and by default this is an empty array.

02:06.170 --> 02:06.500
Okay.

02:06.530 --> 02:16.430
So now we can do for each and go over the large array where every element that we have.

02:16.430 --> 02:19.700
Well we're going to add to the out array.

02:19.730 --> 02:25.190
Now in PHP the simplest way to add a new element is using such syntax.

02:25.220 --> 02:30.800
This essentially means add a new element at the end of this array.

02:31.490 --> 02:36.050
And in this case that's just value times two.

02:36.680 --> 02:43.970
And additionally I think that we can also keep track of the used memory.

02:46.250 --> 02:48.140
And this can also be used.

02:48.140 --> 02:50.630
This can also be done by using function.

02:50.630 --> 02:54.140
This is memory get usage.

02:54.860 --> 03:05.000
And also at the end we can get the end time by just running Microtime with the true parameter and also

03:05.030 --> 03:07.160
the end memory.

03:07.190 --> 03:10.940
Let's get it using memory.

03:13.130 --> 03:15.380
Get usage.

03:16.880 --> 03:17.330
All right.

03:17.330 --> 03:21.110
And now let's output the results.

03:22.850 --> 03:25.520
So first we need to display the time.

03:25.520 --> 03:35.090
And let's concatenate the results of subtracting the end time or subtracting the start time from the

03:35.090 --> 03:36.200
end time.

03:40.610 --> 03:43.340
So this would be in seconds.

03:43.790 --> 03:45.650
That's the first result.

03:46.220 --> 03:50.810
And another result is the memory.

03:51.830 --> 03:55.250
So time and memory.

03:58.010 --> 04:02.520
So here let's round the result using the round function.

04:02.550 --> 04:09.660
Since the result would be in bytes, let's also convert that to megabytes.

04:10.350 --> 04:16.230
So first we do end memory minus start memory.

04:16.260 --> 04:18.690
I think it's called start mem.

04:19.560 --> 04:21.450
Let's rename that to memory.

04:25.950 --> 04:26.820
All right.

04:27.510 --> 04:34.650
And then we divide that by 1024 to convert to kilobytes.

04:34.650 --> 04:39.480
And again 1024 converting to megabytes.

04:39.510 --> 04:48.540
Quick reminder 1024 bytes makes one kilobyte 1024kB makes one megabyte.

04:49.110 --> 04:52.230
Also, let's adjust this to be megabytes.

04:52.230 --> 04:53.460
And now let's run that.

04:53.460 --> 04:56.400
So this is for each dash ref.

04:56.460 --> 04:58.800
And here are the results.

04:58.800 --> 05:01.260
That's the time it took to process the array.

05:01.260 --> 05:03.540
And that was the memory usage.

05:03.570 --> 05:05.580
16MB.

05:05.610 --> 05:09.780
Now let's see, what will we have with the references.

05:09.810 --> 05:14.640
Now keep in mind that here we had to have two arrays.

05:14.640 --> 05:23.160
So the large array and this outer array that just contained the results of those calculations.

05:23.970 --> 05:27.750
Now let's modify this example to use references.

05:27.750 --> 05:34.620
So with loops you can use references by adding the end sign before this variable.

05:34.740 --> 05:42.360
So what this does with the loops is that instead of creating a copy of every element of the array that

05:42.360 --> 05:48.180
we go over, we actually directly reference it.

05:48.210 --> 05:49.500
So now.

05:52.800 --> 06:00.090
If you do something like this, you are just gonna modify an entry inside this original array.

06:00.120 --> 06:07.380
So this means with this example we don't need the out array as we are going to be modifying the original

06:07.380 --> 06:08.040
value.

06:09.480 --> 06:12.360
Okay, so let me run this example.

06:12.360 --> 06:15.030
So let's quickly go over the results.

06:15.090 --> 06:18.030
That's the time without references.

06:18.210 --> 06:21.150
And this is the time with references.

06:21.150 --> 06:23.820
So with references it's slower.

06:23.820 --> 06:25.350
It took more time.

06:25.350 --> 06:29.250
That's the memory without using references in loops.

06:29.310 --> 06:33.270
And that's the memory used with references.

06:33.270 --> 06:36.570
So it's almost double the memory.

06:36.750 --> 06:39.780
That's why I'm saying this.

06:39.780 --> 06:45.090
Be careful if you try to use references for optimizations.

06:45.090 --> 06:53.670
So in this case using reference lets you directly modify the value in the original array, which wouldn't

06:53.670 --> 06:57.390
be possible otherwise with the for each loop.

06:57.390 --> 06:59.310
So that's the use case here.

06:59.340 --> 07:02.820
Not any potential optimizations.
