C ++定时,从上一个整数秒开始的毫秒数 [英] C++ timing, milliseconds since last whole second

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

问题描述

我正在使用一个C ++应用程序,需要详细的时间信息,直到毫秒级别。



我们打算使用 time()函数收集时间到第二个准确度>< ctime> 。我们还要收集从 time()给出的最后一秒之后经过的毫秒。有没有人知道一个方便的方法来获取这些信息?



解决方案

Boost.DateTime具有毫秒和纳秒表示,底层平台支持它们。虽然它正在使用特定于平台的代码,但是将这些细节保留在代码之外。



如果这是一个大问题,他们有另一种做独立平台的平台的方法。 此页面几段落下谈论如何做到这一点。



(从页面)



例如,假设我们想使用表示十分之一秒的计数构造。也就是说,每个刻度是0.1秒。

  int number_of_tenths = 5; 
//创建分辨率独立计数 - 除以10,因为有
//一秒钟的十分之一秒。
int count = number_of_tenths *(time_duration :: ticks_per_second()/ 10);
time_duration td(1,2,3,count); //01:02:03.5 //无论分辨率设置


I'm working on a C++ application that needs detailed timing information, down to the millisecond level.

We intend to gather the time to second accuracy using the standard time() function in <ctime>. We would like to additionally gather the milliseconds elapsed since the last second given by time().

Does anyone know a convenient method for obtaining this information?

解决方案

Boost.DateTime has millisecond and nanosecond representations IF the underlying platform supports them. While it is using platform specific code, it is keeping those details out of your code.

If that is a big deal, they do have another way of doing platform independent subsecond resolution. This page a couple of paragraphs down talks about how to do it.

(From the Page)

For example, let's suppose we want to construct using a count that represents tenths of a second. That is, each tick is 0.1 second.

int number_of_tenths = 5;
//create a resolution independent count -- divide by 10 since there are 
//10 tenths in a second.  
int count = number_of_tenths*(time_duration::ticks_per_second()/10);
time_duration td(1,2,3,count); //01:02:03.5 //no matter the resolution settings

这篇关于C ++定时,从上一个整数秒开始的毫秒数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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