WEBVTT

00:00.150 --> 00:05.340
Great to know, you know, how to use the milk and the micro functions to get the time since the beginning

00:05.340 --> 00:06.060
of the program.

00:06.450 --> 00:12.000
What am I going to do in this lesson is to learn how to compute the duration of an election.

00:12.270 --> 00:17.850
And this will help you for the next lesson where we actually solve the problem.

00:18.180 --> 00:23.670
OK, so commuting the duration of connection is very useful for mainly two things.

00:24.000 --> 00:29.600
So first, to solve the problem and also to be able to debug your program.

00:29.610 --> 00:36.660
So if you have an instruction that you think it's quite long and it's making your program lag, then

00:36.660 --> 00:41.820
you can easily check how long that instruction on that block of code takes.

00:42.030 --> 00:47.160
And then if you find out that it takes too long, you can improve your code.

00:47.460 --> 00:49.560
So let's make a quick example here.

00:50.220 --> 00:51.780
I'm going to remove all that.

00:52.110 --> 00:55.860
And the action I'm going to measure is simply a delay.

00:56.730 --> 00:57.570
Let's say delay.

00:57.570 --> 00:58.230
Five hundred.

00:58.890 --> 01:04.830
But this is convenient, for the example, because we know that the DeLay 500 will take five hundred

01:04.830 --> 01:05.440
milliseconds.

01:05.460 --> 01:12.450
OK, so this way we can validate that our program to compute the duration of connection works and then

01:12.450 --> 01:14.040
we can use that on any of them.

01:14.040 --> 01:14.640
Look of code.

01:14.880 --> 01:22.020
So if you want to measure the duration between the line four and line six of the program here, what

01:22.020 --> 01:31.410
you would need to do is first to get the time before the action and then do the action and then get

01:31.410 --> 01:37.800
the time after the action and then simply make the difference between the time after the action and

01:37.800 --> 01:40.740
the time before the action and you get the duration.

01:40.770 --> 01:41.130
All right.

01:41.140 --> 01:47.130
So let's let's simply right the code for that unsigned long time.

01:47.140 --> 01:47.970
I'm going to name it.

01:47.970 --> 01:48.960
Time begin.

01:50.950 --> 01:52.000
We miss.

01:53.540 --> 02:01.460
So here we get in the time being viable, the time just before we execute the action, now we execute

02:01.460 --> 02:06.410
the action and signed long time and.

02:08.510 --> 02:10.410
OK, so that is the exact same thing.

02:10.430 --> 02:16.580
OK, we just use a different label, so here we have the time before the action, we do the action.

02:16.610 --> 02:18.720
We started time after the action.

02:18.820 --> 02:21.770
OK, so it's important to get the time before the action.

02:21.940 --> 02:24.140
OK, so we can compare the two here.

02:24.450 --> 02:30.970
OK, the minutes here will give a different result than the minutes here because 500 milliseconds have

02:30.980 --> 02:35.000
passed in the prior and not to compute the duration.

02:35.000 --> 02:39.300
So the duration will be the time and minus the time being.

02:39.890 --> 02:43.690
OK, the duration will always be positive.

02:44.150 --> 02:51.620
So if it's always positive, I'm going to use unsigned also long duration and then we simply make the

02:51.620 --> 02:55.440
substraction time and minus time.

02:56.510 --> 03:01.340
OK, in this order and we get the duration of the action.

03:01.490 --> 03:02.000
Great.

03:02.600 --> 03:08.300
Let's simply make sure that L.N. duration.

03:10.210 --> 03:16.360
What I could do to make it nicer is maybe to do, say, under Prince the.

03:18.300 --> 03:19.680
Duration is.

03:21.840 --> 03:26.670
Ladies, and then they'll bring and with duration.

03:27.180 --> 03:31.600
So here the duration will be on the same line as the duration is.

03:31.920 --> 03:38.490
And after that, within the next day, Prince William will go to annealing and let's try this.

03:38.490 --> 03:38.830
Cool.

03:38.910 --> 03:43.370
So I'm going to code, must say, a monitor.

03:45.290 --> 03:52.010
The duration is four hundred ninety nine, which is already close to 500, so great with this goal,

03:52.370 --> 03:56.870
you can get the duration of any action of any block of code.

03:57.680 --> 04:04.460
Now, you can also use macros like Westing we've seen before, the micros.

04:05.430 --> 04:12.360
Microsoft, so microbes also stored on unsign long, so you can just keep eating them like this going

04:12.390 --> 04:16.710
to upload and you will see now the duration is.

04:18.170 --> 04:28.220
Five, we so 500000, which is also 500 milliseconds, which exactly is what we have in the delay here.

04:29.030 --> 04:33.060
OK, so if you want more precision, you can use Migros, OK?

04:33.200 --> 04:37.820
If you don't want to be more precise than one milliseconds, you can use Milice.

04:42.480 --> 04:48.400
And in these schools, mostly, I will use Ministroke because we don't need to be super extra precise.

04:48.420 --> 04:50.340
OK, one minute is already quite quick.

04:51.000 --> 04:51.350
All right.

04:51.360 --> 04:57.360
So that is the way to get the duration of what action you measure the time before you do the action.

04:57.360 --> 04:58.850
You measure the time after.

04:59.340 --> 05:03.340
Then you get the duration by subtracting the two times.

05:03.360 --> 05:08.950
You can make sure that you when you measure the time is just before and just after the action.

05:08.970 --> 05:10.890
OK, just don't do anything else.

05:10.890 --> 05:12.450
Like don't bring something.

05:12.780 --> 05:14.600
Just measure the action you want to measure.

05:15.300 --> 05:17.420
And I'm going to show you something a little bit different.

05:17.460 --> 05:23.430
OK, so you can actually still get the duration in the loop function from something that you have initialized

05:23.430 --> 05:26.710
outside of the function that just doing simple.

05:26.730 --> 05:27.550
So you can understand.

05:28.500 --> 05:31.560
So I'm going to write here and sign.

05:32.740 --> 05:33.190
Long.

05:34.140 --> 05:35.370
Thank you.

05:37.510 --> 05:46.980
So I just declare Dimebolin, I don't initialize it, let's write this, let's actually commend it,

05:47.710 --> 05:48.550
we don't need that.

05:49.510 --> 05:55.210
He insulated the cell communication they do time biggin equal to.

05:55.460 --> 06:01.270
Maybe I could just have initialised images here that would be the same because Millet's would be zero

06:01.270 --> 06:01.840
at the beginning.

06:01.840 --> 06:04.570
OK, and not in the loop I'm going to do.

06:05.960 --> 06:12.170
DeLay one thousand and then and sign long.

06:13.240 --> 06:23.560
Time now is equal to me, so every time I get the time in the loop and then let's say unsigned, long

06:23.800 --> 06:24.580
duration.

06:26.200 --> 06:31.600
Things start is equal to time now minus.

06:32.700 --> 06:39.390
I'm mean, and then a prince say that print and then we duration.

06:43.510 --> 06:45.380
But what I'm what am I doing here?

06:45.850 --> 06:52.150
There is a time that we have initialised, OK, this is a global variable, so it has been initialized

06:52.150 --> 06:53.020
to some value.

06:53.030 --> 06:55.630
So here, for example, zero and then in the loop.

06:55.640 --> 06:59.530
So I do my action in the loop, which takes one second, for example.

07:00.130 --> 07:07.090
And then I measure the time and I measure the duration between the time now in the loop nine, the time

07:07.090 --> 07:07.430
being.

07:07.450 --> 07:14.740
So we get here, we can get the duration also since a certain point of time that we have initialized

07:15.100 --> 07:16.150
outside of the loop.

07:17.570 --> 07:18.590
So let's just bring.

07:20.060 --> 07:21.320
Is just the way does.

07:23.220 --> 07:28.400
And you can see we have one, two, three, four, five thousand, etc..
