WEBVTT

00:00.090 --> 00:00.630
Hello again!

00:00.870 --> 00:03.750
In this video, we are going to look at clocks and time points.

00:05.780 --> 00:08.120
The chrono library provides three clocks.

00:08.630 --> 00:13.610
The system clock is the one that is provided by the system hardware, through the operating system.

00:14.120 --> 00:20.330
This measures the so-called "wall time". So that expression goes back to the days when every office,

00:20.330 --> 00:23.390
factory, laboratory, and so on, had a clock on the wall.

00:25.090 --> 00:27.940
There is a steady clock which is provided in software.

00:28.330 --> 00:32.530
This is an ideal clock which can only go forwards, one tick at a time.

00:33.190 --> 00:37.960
You might think that is how a system clock is supposed to work. But, in fact, it is not quite as simple

00:37.960 --> 00:38.380
as that.

00:41.380 --> 00:43.750
There is also a high resolution clock.

00:44.170 --> 00:48.520
This is guaranteed to support the shortest possible tick period on that system.

00:49.330 --> 00:55.720
How it does that, is implementation is defined. And usually library will make life easy for themselves.

00:56.350 --> 00:59.950
So they implement one of these two, using the highest possible precision.

01:00.430 --> 01:02.770
And then the high-resolution clock is just an alias.

01:03.520 --> 01:07.180
So in fact, it is not very useful, because you do not know which one you are going to get.

01:11.460 --> 01:15.180
The system clock is similar to the one which comes with the C library.

01:15.630 --> 01:19.140
This will measure the "wall time", using the operating system clock.

01:19.980 --> 01:26.460
So if the computer is correctly synchronized, this will be the same time as measured by calendars,

01:26.460 --> 01:28.110
watches, phones and so on.

01:29.190 --> 01:35.130
So this makes it the best clock for interactive use, because it corresponds to what the user thinks the

01:35.130 --> 01:35.610
time is.

01:38.180 --> 01:42.470
However, it's not good for measuring time intervals, because the clock can be changed.

01:43.160 --> 01:49.730
For example, if the clock runs fast or is running slow, then that could be corrected. Either by the computer

01:49.730 --> 01:52.490
operator, or by time synchronization software.

01:53.390 --> 01:58.070
Every now and again, the time changes, because of daylight savings or leap seconds.

01:58.790 --> 02:03.680
So it is possible that if you are in the middle of measuring an interval, the clock could jump forwards.

02:03.920 --> 02:04.730
Or even backwards!

02:05.450 --> 02:07.490
So you get completely the wrong result.

02:10.370 --> 02:15.650
So the best clock for measuring time intervals is the steady clock, because this can only go forwards,

02:16.010 --> 02:18.560
and it can only go up by one tick at a time.

02:18.950 --> 02:24.020
So this is a bit like the program having its own internal stopwatch, and this will tick away constantly,

02:24.320 --> 02:27.860
regardless of what the time is doing in the outside world.

02:30.730 --> 02:31.990
To use these clocks,

02:31.990 --> 02:38.740
we have a static function called now(). And this will return the current timepoint, as seen by that clock.

02:39.280 --> 02:43.150
So system_clock::now() for the hardware clock, and steady_clock::

02:43.150 --> 02:44.950
now() for the steady clock.

02:48.090 --> 02:51.060
The time_point class represents a point in time.

02:51.600 --> 02:58.410
It has a member, which is a duration, and that duration corresponds to the time interval since the

02:58.410 --> 03:01.350
clock's epoch. Since the clock started ticking.

03:01.830 --> 03:04.650
So for the C library clock, that would be 1970.

03:06.380 --> 03:08.950
A time_point does not exist in isolation.

03:08.960 --> 03:11.000
It is associated with a particular clock.

03:11.750 --> 03:16.310
And to get a time_point, we call the now() function for that clock.

03:18.830 --> 03:22.730
We can subtract 2 time_points, and that will give us a duration.

03:22.820 --> 03:29.260
So that will be the interval between those two time points. And we can also add a time_point and a duration

03:29.270 --> 03:31.010
together, or subtract them.

03:31.250 --> 03:33.290
So that will give us a new time_point.

03:37.040 --> 03:42.710
As an example, I am going to measure how long it takes to calculate a number in the Fibonacci sequence.

03:43.610 --> 03:48.350
I am going to call the now() function just before the calculation, and just afterwards.

03:48.830 --> 03:53.330
So that will give me the starting time_point and the finishing time_point for the calculation.

03:54.720 --> 03:59.430
I'm using the steady_clock, because that is the best one to use for measuring intervals.

04:00.510 --> 04:03.240
The Fibonacci calculation, I am sure you're all familiar with.

04:04.830 --> 04:05.850
The number to use.

04:05.850 --> 04:07.410
You may need to tune this a bit.

04:07.890 --> 04:14.820
If I am running on the command line in release mode, then 45 is about right. For Visual Studio in debug mode,

04:15.540 --> 04:19.920
that takes a lot longer. Because, of course, it has to support all the debugging functionality.

04:20.220 --> 04:22.170
So the number 40 works better for me.

04:23.730 --> 04:27.210
And then, I subtract the start time_point from the finish.

04:27.630 --> 04:31.170
So that will give me the duration, how long it took for the calculation.

04:32.610 --> 04:35.210
Then I want to get the result in milliseconds.

04:35.220 --> 04:41.220
So I do a duration_cast with milliseconds as the parameter, and this duration as the argument.

04:41.850 --> 04:46.710
And then I call the count() member function of the duration, to get the number of milliseconds.

04:47.370 --> 04:48.540
So let's see what we get.

04:51.860 --> 04:54.970
This should take about 5 seconds or maybe six.

04:54.980 --> 04:56.780
So about now, I think.

04:57.530 --> 04:58.010
And there we go.

04:58.490 --> 05:00.140
So that is the Fibonacci number.

05:00.710 --> 05:04.850
And the time taken was 5463 milliseconds.

05:07.140 --> 05:09.390
And, finally, the sleep_for() function.

05:09.840 --> 05:13.560
I have mentioned this a couple of times, so perhaps we should cover it, just to make sure.

05:14.250 --> 05:21.360
So, in C++11, it is now possible to make a program, or a thread, sleep for a specified duration.

05:24.640 --> 05:26.620
This came in with the thread library.

05:27.550 --> 05:29.500
There is a static function.

05:29.950 --> 05:34.690
this_thread::sleep_for(). And this will apply to the currently executing thread.

05:35.290 --> 05:39.670
And this thread will pause, or "sleep", for the duration of its argument.

05:41.350 --> 05:46.120
If you are using a single-threaded program, this will make the main thread sleep.

05:47.920 --> 05:49.630
The duration is only a minimum.

05:49.780 --> 05:53.350
What happens, is that this program, or the thread, will stop executing.

05:53.740 --> 05:59.620
So the operating system scheduler or the thread scheduler will start doing other things. And it will

05:59.620 --> 06:02.680
not come back to this, until all those other things are finished.

06:02.950 --> 06:05.110
And the scheduler thinks that you were due for another turn.

06:05.620 --> 06:08.470
So it is possible that the delay may be more 2 seconds.

06:10.990 --> 06:14.260
So let's quickly try that out. "Waiter?"

06:15.270 --> 06:15.830
"You called, sir?"

06:16.380 --> 06:16.710
"Yes!

06:16.710 --> 06:17.700
There is a fly in my soup!"

06:18.600 --> 06:19.350
"Do not say that, sir."

06:19.380 --> 06:19.890
"They will all want

06:19.890 --> 06:20.130
one!"

06:21.180 --> 06:21.480
Okay.

06:21.480 --> 06:22.560
So that is it for this video!

06:22.980 --> 06:23.820
I will see you next time.

06:23.820 --> 06:25.980
But until then, keep coding!
