简体   繁体   English

如何从boost :: chrono :: hight_resolution_clock :: time_point获得纳秒?

[英]How to get nanoseconds from boost::chrono::hight_resolution_clock::time_point?

I am new to boost and chrono. 我是新来的Boost和Chrono。 I am writing a logger that logs the timestamps of API calls, entry and exit. 我正在编写一个记录器,记录API调用,进入和退出的时间戳。 I tried using boost::xtime first, but it wasn't giving the high resolution value I needed. 我尝试首先使用boost :: xtime,但是它没有提供我需要的高分辨率值。 Hence was thinking about using Chrono. 因此正在考虑使用Chrono。 I declared a boost::chrono::hight_resolution_clock::time_stamp x; 我宣布了boost :: chrono :: hight_resolution_clock :: time_stamp x; variable for getting the timestamp and assigned it to boost::chrono::hight_resolution_clock::now ();. 用于获取时间戳并将其分配给boost :: chrono :: hight_resolution_clock :: now();的变量。 Now, I need to get the nanoseconds from this variable and put it in my log file (thats the requirement). 现在,我需要从此变量中获取纳秒级的数据,并将其放入我的日志文件中(这是必需的)。 So I cast it boost::chrono::duration_cast (x). 因此,我将其投放为boost :: chrono :: duration_cast(x)。 But it just wouldn't let me do that. 但这只是不允许我这样做。 It needs 2 parameters apparently, and I only have one. 显然,它需要2个参数,而我只有一个。 Is there a way to get around this?. 有没有办法解决这个问题? Is it possible to create another time_stamp variable and assign zero to it and use that variable?. 是否可以创建另一个time_stamp变量并为其分配零并使用该变量? I tried assigning zero, but its not working. 我尝试分配零,但无法正常工作。 Kindly help me out. 请帮我。

Thanks, Sam 谢谢山姆

If tagged c++11 , any reason why not to use std::chrono ? 如果标记为c++11 ,为什么不使用std::chrono呢?

// Using std::chrono
auto start = std::chrono::high_resolution_clock::now();  // start timer

/* do some work */

auto diff = std::chrono::high_resolution_clock::now() - start; // get difference 
auto nsec = std::chrono::duration_cast<std::chrono::nanoseconds>(diff);
std::cout << "it took: " << nsec.count() << " nanoseconds" << std::endl;

boost::chrono::duration_cast converts a duration into the specified units, but you've given it a boost::chrono::time_point , not a duration. boost::chrono::duration_cast将持续时间转换为指定的单位,但是您给了它boost::chrono::time_point ,而不是持续时间。

There's really no such thing as " the current time in nanoseconds ". 确实没有“ 当前时间(以纳秒为单位)”这样的东西。 To get a duration, you need to specify the time since which you want to know how many nanoseconds have elapsed (an "epoch"). 要获得持续时间,您需要指定一个时间,从该时间开始,您要知道已经过去了多少纳秒(一个“时期”)。 Different clocks will measure their time based on different epochs. 不同的时钟将根据不同的时间段来测量时间。

boost::chrono::system_clock (currently) uses the Unix epoch (midnight Jan 1, 1970) as its epoch, but it's not steady and it may not have the resolution you need (it's in nanoseconds on my Ubuntu box, but in 1/10,000,000ths of a second on my Windows box). boost::chrono::system_clock (当前)使用Unix纪元(1970年1月1日午夜)作为其纪元,但它不是稳定的,并且可能没有所需的分辨率(在我的Ubuntu盒子中为纳秒,但在1中在我的Windows机器上为/ 10,000,000秒。

boost::chrono::high_resolution_clock uses boot up as its epoch, is steady, and measures time in nanoseconds on both boxes I tested on. boost::chrono::high_resolution_clock使用启动作为其时代,它是稳定的,并且在我测试过的两个盒子上都以纳秒为单位测量时间。

Boost also provides other clocks like process_cpu_clock that use other epochs and count in other units. Boost还提供其他时钟,例如process_cpu_clock ,它们使用其他纪元并以其他单位计数。

Thus you can get nanos since Jan 1, 1970 using system_clock , but it may not actually be nanosecond-accurate, and it may go backwards if the user changes the system time or the computer syncs with network time, or you can get nanos since some other point in time using high_resolution_clock . 因此,您可以从1970年1月1日开始使用system_clock获得nanos,但实际上可能不是十亿分之一秒,如果用户更改系统时间或计算机与网络时间同步,它可能会倒退,或者由于某些原因您可以获取nanos。使用high_resolution_clock其他时间点。

暂无
暂无

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

相关问题 boost :: chrono :: time_point &lt;&gt;和boost :: chrono :: steady_clock :: time_point之间的区别 - Difference between boost::chrono::time_point<> and boost::chrono::steady_clock::time_point 如何获取std :: chrono :: time_point时钟类型 - How to get std::chrono::time_point clock type boost :: serialize和std :: chrono :: system_clock :: time_point - boost::serialize and std::chrono::system_clock::time_point 从双创建一个std :: chrono :: high_resolution_clock :: time_point吗? - creating an std::chrono::high_resolution_clock::time_point from a double? 如何从std :: chrono :: system_clock :: time_point.time_since_epoch()。count()获取std :: chrono :: system_clock :: time_point? - How to get std::chrono::system_clock::time_point from std::chrono::system_clock::time_point.time_since_epoch().count()? 如何获取 std::chrono::time_point<std::chrono::system_clock, std::chrono::seconds> 来自年、月、日、小时、分钟和秒的变量?</std::chrono::system_clock,> - How to get std::chrono::time_point<std::chrono::system_clock, std::chrono::seconds> from variables of year,month,day,hour,minute and seconds? C++ 打印 chrono::time_point<chrono::high_resolution_clock> 可读</chrono::high_resolution_clock> - C++ print chrono::time_point<chrono::high_resolution_clock> to be readable 通过Boost.Chrono以纳秒的分辨率获取时间戳 - Get time stamp via Boost.Chrono in resolution of nanoseconds 标准::原子<std::chrono::high_resolution_clock::time_point>无法编译 - std::atomic<std::chrono::high_resolution_clock::time_point> can not compile 输出boost :: chrono :: system_clock :: time_point作为相对于UTC的本地时间 - Output boost::chrono::system_clock::time_point as local time with respect to UTC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM