Great to know, you know, how to use the milk and the micro functions to get the time since the beginning
of the program.
What am I going to do in this lesson is to learn how to compute the duration of an election.
And this will help you for the next lesson where we actually solve the problem.
OK, so commuting the duration of connection is very useful for mainly two things.
So first, to solve the problem and also to be able to debug your program.
So if you have an instruction that you think it's quite long and it's making your program lag, then
you can easily check how long that instruction on that block of code takes.
And then if you find out that it takes too long, you can improve your code.
So let's make a quick example here.
I'm going to remove all that.
And the action I'm going to measure is simply a delay.
Let's say delay.
Five hundred.
But this is convenient, for the example, because we know that the DeLay 500 will take five hundred
milliseconds.
OK, so this way we can validate that our program to compute the duration of connection works and then
we can use that on any of them.
Look of code.
So if you want to measure the duration between the line four and line six of the program here, what
you would need to do is first to get the time before the action and then do the action and then get
the time after the action and then simply make the difference between the time after the action and
the time before the action and you get the duration.
All right.
So let's let's simply right the code for that unsigned long time.
I'm going to name it.
Time begin.
We miss.
So here we get in the time being viable, the time just before we execute the action, now we execute
the action and signed long time and.
OK, so that is the exact same thing.
OK, we just use a different label, so here we have the time before the action, we do the action.
We started time after the action.
OK, so it's important to get the time before the action.
OK, so we can compare the two here.
OK, the minutes here will give a different result than the minutes here because 500 milliseconds have
passed in the prior and not to compute the duration.
So the duration will be the time and minus the time being.
OK, the duration will always be positive.
So if it's always positive, I'm going to use unsigned also long duration and then we simply make the
substraction time and minus time.
OK, in this order and we get the duration of the action.
Great.
Let's simply make sure that L.N. duration.
What I could do to make it nicer is maybe to do, say, under Prince the.
Duration is.
Ladies, and then they'll bring and with duration.
So here the duration will be on the same line as the duration is.
And after that, within the next day, Prince William will go to annealing and let's try this.
Cool.
So I'm going to code, must say, a monitor.
The duration is four hundred ninety nine, which is already close to 500, so great with this goal,
you can get the duration of any action of any block of code.
Now, you can also use macros like Westing we've seen before, the micros.
Microsoft, so microbes also stored on unsign long, so you can just keep eating them like this going
to upload and you will see now the duration is.
Five, we so 500000, which is also 500 milliseconds, which exactly is what we have in the delay here.
OK, so if you want more precision, you can use Migros, OK?
If you don't want to be more precise than one milliseconds, you can use Milice.
And in these schools, mostly, I will use Ministroke because we don't need to be super extra precise.
OK, one minute is already quite quick.
All right.
So that is the way to get the duration of what action you measure the time before you do the action.
You measure the time after.
Then you get the duration by subtracting the two times.
You can make sure that you when you measure the time is just before and just after the action.
OK, just don't do anything else.
Like don't bring something.
Just measure the action you want to measure.
And I'm going to show you something a little bit different.
OK, so you can actually still get the duration in the loop function from something that you have initialized
outside of the function that just doing simple.
So you can understand.
So I'm going to write here and sign.
Long.
Thank you.
So I just declare Dimebolin, I don't initialize it, let's write this, let's actually commend it,
we don't need that.
He insulated the cell communication they do time biggin equal to.
Maybe I could just have initialised images here that would be the same because Millet's would be zero
at the beginning.
OK, and not in the loop I'm going to do.
DeLay one thousand and then and sign long.
Time now is equal to me, so every time I get the time in the loop and then let's say unsigned, long
duration.
Things start is equal to time now minus.
I'm mean, and then a prince say that print and then we duration.
But what I'm what am I doing here?
There is a time that we have initialised, OK, this is a global variable, so it has been initialized
to some value.
So here, for example, zero and then in the loop.
So I do my action in the loop, which takes one second, for example.
And then I measure the time and I measure the duration between the time now in the loop nine, the time
being.
So we get here, we can get the duration also since a certain point of time that we have initialized
outside of the loop.
So let's just bring.
Is just the way does.
And you can see we have one, two, three, four, five thousand, etc..