简体   繁体   English

将高分辨率时钟时间转换为整数(Chrono)

[英]Convert high resolution clock time into an integer (Chrono)

I want to be able to get nanosecond accuracy with the chrono library but I can't figure out how to convert std::chrono::high_resolution_clock::now() into long int . 我希望能够使用chrono库获得纳秒精度,但我无法弄清楚如何将std::chrono::high_resolution_clock::now()转换为long int I tried this: 我试过这个:

#include <chrono>
#include <iostream>
using namespace std;

int main() {
    typedef std::chrono::high_resolution_clock Clock;

    long int val = Clock::now();

    cout << val << endl;

    cin.ignore();
    return 0;
}

But this gave me the error: error C2440: 'initializing' : cannot convert from 'std::chrono::system_clock::time_point' to 'long' How can I convert it to a 64 bit int? 但这给了我错误: error C2440: 'initializing' : cannot convert from 'std::chrono::system_clock::time_point' to 'long'转换error C2440: 'initializing' : cannot convert from 'std::chrono::system_clock::time_point' to 'long'如何将其转换为64位int? If I can't then I don't see how chrono is useful. 如果我不能,那么我不知道计时器是如何有用的。

The following works with GCC 4.8 on Linux: 以下适用于Linux上的GCC 4.8:

using namespace std::chrono;
auto now = high_resolution_clock::now();
auto nanos = duration_cast<nanoseconds>(now.time_since_epoch()).count();
std::cout << nanos << '\n';

First, convert the time point returned by now() into the duration since a known timepoint. 首先,将now()返回的时间点转换为自已知时间点以来的持续时间。 This can either be the clock's epoch: 这可以是时钟的时代:

auto since_epoch = Clock::now().time_since_epoch();

or some timepoint that you've chosen: 或者您选择的某个时间点:

auto since_epoch = Clock::now() - my_epoch;

Then you can get the number of nanoseconds either by converting and extracting the count: 然后,您可以通过转换和提取计数来获得纳秒数:

auto nanos = duration_cast<nanoseconds>(since_epoch).count();

or by dividing by whatever granularity you want: 或者通过划分您想要的任何粒度:

auto nanos = since_epoch / nanoseconds(1);

As noted in the comments, only do this last conversion (which leaves the Chrono library's type system, losing valuable information about what the number means) if you really need a scalar quantity; 正如评论中所指出的那样,如果你真的需要一个标量,那么只进行最后一次转换(离开Chrono库的类型系统,丢失有关数字含义的有价值信息); perhaps because you're interacting with an API that doesn't use the standard types. 也许是因为您正在与不使用标准类型的API进行交互。 For your own calcuations, the types should allow you to perform any meaningful arithmetic that you need. 对于您自己的计算,类型应该允许您执行您需要的任何有意义的算术。

一个更简洁的版本的nosid的答案:

long int time = static_cast<long int>(std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count());

You can use a std::chrono::duration_cast: 你可以使用std :: chrono :: duration_cast:

http://en.cppreference.com/w/cpp/chrono/duration/duration_cast http://en.cppreference.com/w/cpp/chrono/duration/duration_cast

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++:将chrono :: high_resolution_clock转换为time_t - C++: convert chrono::high_resolution_clock to time_t std :: chrono :: high_resolution_clock-时间测量 - std::chrono::high_resolution_clock - time measurement 时间差与std :: chrono :: system_clock / std :: chrono :: high_resolution_clock - time differences with std::chrono::system_clock / std::chrono::high_resolution_clock C++ 打印 chrono::time_point<chrono::high_resolution_clock> 可读</chrono::high_resolution_clock> - C++ print chrono::time_point<chrono::high_resolution_clock> to be readable chrono :: high_resolution_clock延迟不一致 - Inconsistent chrono::high_resolution_clock delay 比较rdtsc clock和c ++ 11 std :: chrono :: high_resolution_clock产生的时间测量结果 - Comparing the time measured results produced by rdtsc clock and c++11 std::chrono::high_resolution_clock 如何将 std::chrono::high_resolution_clock::now() 转换为毫秒、微秒...? - How to convert std::chrono::high_resolution_clock::now() to milliseconds, microseconds, ...? 标准::原子<std::chrono::high_resolution_clock::time_point>无法编译 - std::atomic<std::chrono::high_resolution_clock::time_point> can not compile 从双创建一个std :: chrono :: high_resolution_clock :: time_point吗? - creating an std::chrono::high_resolution_clock::time_point from a double? 奇怪的行为使用chrono :: high_resolution_clock :: now() - odd behaviour using chrono::high_resolution_clock::now()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM