C++ std::chrono 和 cout

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7889136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:39:07  来源:igfitidea点击:

std::chrono and cout

c++timec++11chrono

提问by NoSenseEtAl

I have a stupid problem. I try to switch to the c++11 headers and one of those is chrono. But my problem is that I cant cout the result of time operations. For example:

我有一个愚蠢的问题。我尝试切换到 c++11 头文件,其中之一是 chrono。但我的问题是我无法计算时间操作的结果。例如:

auto t=std::chrono::high_resolution_clock::now();
cout<<t.time_since_epoch();

gives:

给出:

initializing argument 1 of ‘std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char, _Traits = std::char_traits<char>, _Tp = std::chrono::duration<long int, std::ratio<1l, 1000000l> >]'... /usr/include/c++/4.6/ostream

正在初始化‘std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char, _Traits = std::char_traits<char>, _Tp = std::chrono::duration<long int, std::ratio<1l, 1000000l> >]'... /usr/include/c++/4.6/ostream 的 参数 1

cout<<(uint64_t)t.time_since_epoch();

gives invalid cast

给出无效演员表

采纳答案by Some programmer dude

A quick google search found this page: http://en.cppreference.com/w/cpp/chrono/duration, where you can find an example of printing a duration.

一个快速的谷歌搜索找到了这个页面:http: //en.cppreference.com/w/cpp/chrono/duration,在那里你可以找到一个打印持续时间的例子。

Edit: it got moved to http://en.cppreference.com/w/cpp/chrono/duration/duration_cast

编辑:它已移至http://en.cppreference.com/w/cpp/chrono/duration/duration_cast

回答by Howard Hinnant

As others have noted, you can call the count()member function to get the internal count.

正如其他人所指出的,您可以调用count()成员函数来获取内部计数。

I wanted to add that I am attempting to add a new header: <chrono_io>to this library. It is documented here. The main advantage of <chrono_io>over just using count()is that the compile-time units are printed out for you. This information is of course obtainable manually, but it is much easier to just have the library to it for you.

我想补充一点,我正在尝试添加一个新的标题:<chrono_io>到这个库。它记录在此处<chrono_io>仅使用的主要优点count()是编译时单位会为您打印出来。这些信息当然可以手动获取,但是为您提供库要容易得多。

For me, your example:

对我来说,你的例子:

#include <iostream>
#include <chrono_io>

int main()
{
    auto t = std::chrono::high_resolution_clock::now();
    std::cout << t.time_since_epoch() << '\n';
}

Outputs:

输出:

147901305796958 nanoseconds

The source code to do this is open source and available at the link above. It consists of two headers: <ratio_io>and <chrono_io>, and 1 source: chrono_io.cpp.

执行此操作的源代码是开源的,可在上面的链接中找到。它由两个标题:<ratio_io><chrono_io>,和 1 个源:chrono_io.cpp

This code should be considered experimental. It is not standard, and almost certainly will not be standardized as is. Indeed preliminary comments from the LWG indicate that they would prefer the default output to be what this software calls the "short form". This alternative output can be obtained with:

这段代码应该被认为是实验性的。它不是标准的,几乎肯定不会按原样标准化。事实上,来自 LWG 的初步意见表明,他们更喜欢默认输出是该软件所称的“简短格式”。可以通过以下方式获得此替代输出:

std::cout << std::chrono::duration_fmt(std::chrono::symbol)
          << t.time_since_epoch() << '\n';

And outputs:

和输出:

147901305796958 ns

回答by Yonatan Simson

If you want timing in resolution of milliseconds this is how you can do it:

如果您想以毫秒为分辨率计时,您可以这样做:

auto t1 = std::chrono::high_resolution_clock::now();
//process to be timed
auto t2 = std::chrono::high_resolution_clock::now();
std::cout << "process took: "
    << std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1).count()
    << " milliseconds\n";

Don't forget to add among the included headers:

不要忘记在包含的标题中添加:

#include <chrono> //timing

回答by Michael Krelin - hacker

Not sure what you expect from this cast, maybe you wanted t.time_since_epoch().count()?

不确定你对这个演员的期望,也许你想要t.time_since_epoch().count()