c++获取当前秒 毫秒 微秒 纳秒时间

c++获取当前秒 毫秒 微秒 纳秒时间,便于算法运行时间比较。

// 获取当前时间的时间戳
#define _CRT_SECURE_NO_WARNINGS
#include 
#include 
#include 

int main()
{
    auto currentTime = std::chrono::system_clock::now();
    auto currentTime_s = std::chrono::time_point_cast<std::chrono::seconds>(currentTime);
    auto currentTime_ms = std::chrono::time_point_cast<std::chrono::milliseconds>(currentTime);
    auto currentTime_micro = std::chrono::time_point_cast<std::chrono::microseconds>(currentTime);
    auto currentTime_ns = std::chrono::time_point_cast<std::chrono::nanoseconds>(currentTime);
    auto valueS = currentTime_s.time_since_epoch().count();
    auto valueMS = currentTime_ms.time_since_epoch().count();
    auto valueMicroS = currentTime_micro.time_since_epoch().count();
    auto valueNS = currentTime_ns.time_since_epoch().count();

    std::cout << "Seconds: " << valueS << std::endl;
    std::cout << "Milliseconds: " << valueMS << std::endl;
    std::cout << "Microseconds: " << valueMicroS << std::endl;
    std::cout << "Nanoseconds: " << valueNS << std::endl;

    //std::cout << "time_s_mils_mics: " << valueS << "  " << valueMS << "  " << valueMicroS << std::endl;

    return 0;
}

你可能感兴趣的:(c++,开发语言,算法)