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

2023-12-13 20:36:52

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

// 获取当前时间的时间戳
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <chrono>
#include <thread>

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;
}

文章来源:https://blog.csdn.net/LW_12345/article/details/134871694
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。