C++ 中的实际毫秒数 [英] Actual millis in a C++

查看:28
本文介绍了C++ 中的实际毫秒数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能获得实际的毫秒数,因为我不知道在 C++ 程序(如 Java 中的 System.currentTimeMillis() )中?我知道 time(),但我认为这不足以测量短时间,是吗?

Is it possible to get the actual millis since I-don't-know in a C++-programm like System.currentTimeMillis() in Java? I know time(), but I think it's not exactly enough to measure short times, is it?

推荐答案

如今(几年后)这是语言标准的一部分:

It's part of the language standard these days (some years now):

看到它在 Coliru 上直播

See it Live On Coliru

#include <chrono>
#include <iostream>

int main()
{
    using namespace std::chrono;

    auto epoch = high_resolution_clock::from_time_t(0);
    // ...
    auto now   = high_resolution_clock::now();

    auto mseconds = duration_cast<milliseconds>(now - epoch).count();

    std::cout << "millis: " << mseconds;
}

这篇关于C++ 中的实际毫秒数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆