C + + Time

Source: Internet
Author: User

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 (&AMP;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.