![]() The details will follow in the next post. With the component’s time point and time duration, you can put a thread for an absolute or relative time to sleep. That is not the end of the story about the new time library. On Linux, the std::chrono::system_clock is internally used on Windows, the std::chrono::steady_clock is internally used. ![]() The difference between Linux and Windows is std::high_resolution_clock. The starting point of std::chrono::steady_clock is the boot time of my PC. Std::chrono::system_clock and std::chrono::high_resolution_clock have on Linux the UNIX-epoch as starting point. To draw the right conclusion, I must mention that my Linux PC runs for about 5 hours (305 minutes) and my Windows PC for more than 6 hours (391 minutes). The results are different on Linux and Windows. I ignore leap years for simplicity, and my year has 365 days. First, I display the number of time ticks (line 9), then the number of minutes (line 13), and at the end of the years (line 16) since the epoch, all depending on the used clock. In the function durationSinceEpoch (lines 7 – 17), I show the time duration in different resolutions. When I use no automatic type deduction with auto, explicit types of the time point and time duration are extremely verbose to write. The variables timeDurSysClock (line 24), timeNowStClock (line 31), and timeNowHiResClock (Zeile 38) hold for each clock, how much time has passed since the starting point of the clock. Std::cout << "std::chrono::high_resolution_clock: " << std::endl Std::cout << "std::chrono::steady_clock: " << std::endl Īuto timeNowHiRes = std::chrono::high_resolution_clock::now() Īuto timeDurHiResClock= timeNowHiRes.time_since_epoch() Std::cout << "std::chrono::system_clock: " << std::endl Īuto timeNowStClock = std::chrono::steady_clock::now() Īuto timeDurStClock= timeNowStClock.time_since_epoch() Std::chrono::system_clock::duration timeDurSysClock= timeNowSysClock.time_since_epoch() Std::chrono::system_clock::time_point timeNowSysClock = std::chrono::system_clock::now() Std::cout << " Years since epoch: " << unt() << std::endl now.cpp #include #include #include template Thanks to the auxiliary function time_since_epoch, you get how much time has passed since the epoch from each time point. std::chrono::system_clock is far more accurate on Linux std::chrono::high_resultion_clock is steady on Windows.Īlthough the C++ standard doesn’t specify the epoch of the clock, you can calculate it. The output on Linux differs from that on Windows. ![]() You can get the details on the calculation at compile time at. Therefore, I use the function template std::ratio_multiply and the constants std::kilo and std::mega to adjust the units to milliseconds and microseconds. First, I show the accuracy of the clocks in a fraction, and second, in a floating number. My job in the function printRatio (lines 7 – 15) is more challenging. ![]() I display in lines 22, 28, and 34 whether each clock is continuous. Std::cout (MillSec::num)/MillSec::den (MicroSec::num)/MicroSec::den () Typedef typename std::ratio_multiply::type MicroSec clockProperties.cpp #include #include #include template It’s interesting to know which clocks are steady and which accuracy they provide. For std::chrono::steady_clock, typically the boot time of your PC. Typically, the starting point of std::chrono:system_clock is the, the so-called UNIX-epoch. The C++ standard provides no guarantee about the clocks’ accuracy, starting point, or valid time range. std::chrono::high_resolution_clock: This is the clock with the highest accuracy, but it can be a synonym for the clock’s std::chrono::system_clock or std::chrono::steady_clock.Therefore, std::chrono::steady_clock is the preferred clock to wait for a time duration or until a time point. std::chrono::steady_clock: Provides as only a clock the guarantee that you can not adjust it.The clock has the auxiliary functions to_time_t and from_time_t to convert time points into dates. std::chrono::sytem_clock: This is the system-wide real-time clock ( wall-clock).C++ offers with std::chrono::system_clock, std::chrono::steady_clock, and std::chrono::high_resolution_clock three clocks.īecause of three different clocks, there is the question: What are the differences? A clock consists of a starting point and a time tick.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |