Hello again!
In this video, we are going to look at clocks and time points.
The chrono library provides three clocks.
The system clock is the one that is provided by the system hardware, through the operating system.
This measures the so-called "wall time". So that expression goes back to the days when every office,
factory, laboratory, and so on, had a clock on the wall.
There is a steady clock which is provided in software.
This is an ideal clock which can only go forwards, one tick at a time.
You might think that is how a system clock is supposed to work. But, in fact, it is not quite as simple
as that.
There is also a high resolution clock.
This is guaranteed to support the shortest possible tick period on that system.
How it does that, is implementation is defined. And usually library will make life easy for themselves.
So they implement one of these two, using the highest possible precision.
And then the high-resolution clock is just an alias.
So in fact, it is not very useful, because you do not know which one you are going to get.
The system clock is similar to the one which comes with the C library.
This will measure the "wall time", using the operating system clock.
So if the computer is correctly synchronized, this will be the same time as measured by calendars,
watches, phones and so on.
So this makes it the best clock for interactive use, because it corresponds to what the user thinks the
time is.
However, it's not good for measuring time intervals, because the clock can be changed.
For example, if the clock runs fast or is running slow, then that could be corrected. Either by the computer
operator, or by time synchronization software.
Every now and again, the time changes, because of daylight savings or leap seconds.
So it is possible that if you are in the middle of measuring an interval, the clock could jump forwards.
Or even backwards!
So you get completely the wrong result.
So the best clock for measuring time intervals is the steady clock, because this can only go forwards,
and it can only go up by one tick at a time.
So this is a bit like the program having its own internal stopwatch, and this will tick away constantly,
regardless of what the time is doing in the outside world.
To use these clocks,
we have a static function called now(). And this will return the current timepoint, as seen by that clock.
So system_clock::now() for the hardware clock, and steady_clock::
now() for the steady clock.
The time_point class represents a point in time.
It has a member, which is a duration, and that duration corresponds to the time interval since the
clock's epoch. Since the clock started ticking.
So for the C library clock, that would be 1970.
A time_point does not exist in isolation.
It is associated with a particular clock.
And to get a time_point, we call the now() function for that clock.
We can subtract 2 time_points, and that will give us a duration.
So that will be the interval between those two time points. And we can also add a time_point and a duration
together, or subtract them.
So that will give us a new time_point.
As an example, I am going to measure how long it takes to calculate a number in the Fibonacci sequence.
I am going to call the now() function just before the calculation, and just afterwards.
So that will give me the starting time_point and the finishing time_point for the calculation.
I'm using the steady_clock, because that is the best one to use for measuring intervals.
The Fibonacci calculation, I am sure you're all familiar with.
The number to use.
You may need to tune this a bit.
If I am running on the command line in release mode, then 45 is about right. For Visual Studio in debug mode,
that takes a lot longer. Because, of course, it has to support all the debugging functionality.
So the number 40 works better for me.
And then, I subtract the start time_point from the finish.
So that will give me the duration, how long it took for the calculation.
Then I want to get the result in milliseconds.
So I do a duration_cast with milliseconds as the parameter, and this duration as the argument.
And then I call the count() member function of the duration, to get the number of milliseconds.
So let's see what we get.
This should take about 5 seconds or maybe six.
So about now, I think.
And there we go.
So that is the Fibonacci number.
And the time taken was 5463 milliseconds.
And, finally, the sleep_for() function.
I have mentioned this a couple of times, so perhaps we should cover it, just to make sure.
So, in C++11, it is now possible to make a program, or a thread, sleep for a specified duration.
This came in with the thread library.
There is a static function.
this_thread::sleep_for(). And this will apply to the currently executing thread.
And this thread will pause, or "sleep", for the duration of its argument.
If you are using a single-threaded program, this will make the main thread sleep.
The duration is only a minimum.
What happens, is that this program, or the thread, will stop executing.
So the operating system scheduler or the thread scheduler will start doing other things. And it will
not come back to this, until all those other things are finished.
And the scheduler thinks that you were due for another turn.
So it is possible that the delay may be more 2 seconds.
So let's quickly try that out. "Waiter?"
"You called, sir?"
"Yes!
There is a fly in my soup!"
"Do not say that, sir."
"They will all want
one!"
Okay.
So that is it for this video!
I will see you next time.
But until then, keep coding!