C + + Time
Header file Chrono, namespace Std.
Now time
Std::chrono::system_clock::now () returns the current time of the system clock
Clock
Std::chrono::system_clock represents the current time of the system, is an unstable clock, and provides a function to convert a point in time to a value of time_t type
Std::chrono::steady_clock represents a stable clock, so-called stability refers to the call now (), whose value is always greater than 1 times.
Delay
Std::this_thread::sleep_for () and Std::this_thread::sleep_until ()
Std::this_thread::sleep_for (std::chrono::milliseconds (100)); Wait 100ms
Get current time
#include <chrono>#include<sstream>#include<iomanip>std::stringGettimestr () {Std::chrono::system_clock::time_point now=Std::chrono::system_clock::now (); time_t TT=std::chrono::system_clock::to_time_t (now); structTM LTM = {0}; localtime_s (&LTM, &TT); Std::stringstream STM; STM<< Std::setfill ('0'); STM<< STD::SETW (4) << (Ltm.tm_year +1900) <<"-"; STM<< STD::SETW (2) << (Ltm.tm_mon +1) <<"-"; STM<< STD::SETW (2) << Ltm.tm_mday <<" "; STM<< STD::SETW (2) << Ltm.tm_hour <<":"; STM<< STD::SETW (2) << Ltm.tm_min <<":"; STM<< STD::SETW (2) <<ltm.tm_sec; returnStm.str (); }
Calculate the time the program code executes
Std::chrono::steady_clock::time_point Tbegin = std::chrono::steady_clock::now (); Do_something (); = Std::chrono::steady_clock::now (); = Std::chrono::d uration_cast<std::chrono::milliseconds> (Tend- tbegin); " used time: " " Ms " << Std::endl;
C + + Time