可移植的方法来获得时间 [英] portable way to get time

查看:143
本文介绍了可移植的方法来获得时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为Mac OS X不支持有时间,我发现这个要点,使得使用clock_gettime基金这里可移植的方式:

Since Mac OS X doesn't support get time, I found this gist that made a portable way to use the clock_gettime fund here:

void current_utc_time(struct timespec *ts) {

#ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
    clock_serv_t cclock;
    mach_timespec_t mts;
    host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
    clock_get_time(cclock, &mts);
    mach_port_deallocate(mach_task_self(), cclock);
    ts->tv_sec = mts.tv_sec;
    ts->tv_nsec = mts.tv_nsec;
#else
  clock_gettime(CLOCK_REALTIME, ts);
#endif
}

使用它像这样

struct timespec requestStart;
current_utc_time(&requestStart);
printf("start: s:  %lu\n", requestStart.tv_sec);
printf("start: ns: %lu\n", requestStart.tv_nsec);
start: s:  1435988139
start: ns: 202015000

我想从这个code得到秒和毫秒的价值,这是正确的方式来获得从纳秒值毫秒?

I am trying to get the seconds and milliseconds value from this code, is this the correct way to get milliseconds from the nanoseconds value?

printf("total: ms:  %lu\n", (requestStart.tv_nsec - requestEnd.tv_nsec) / (unsigned long)1000000);

如果是这样,我怎么秒值?如果没有我怎样才能得到毫秒秒值?

If so, how do I get the seconds value? If not how can I get the milliseconds and seconds value?

修改
在回答第一个评论,我主要是寻找一种方式有code尽可能实现可移植性。

edit In response to the first comment, I am mainly looking for a way to have the code as portable as possible.

推荐答案

如果你想可移植性,只需使用 gettimeofday的。这应该在任何地方工作。 clock_gettime 似乎 Linux的特定一个相对较新的Posix介绍。

If you want portability, just use gettimeofday. That should work everywhere. clock_gettime appears to be Linux-specific a relatively recent Posix introduction.

在转换: timeval结构(使用 gettimeofday的和其他老功能)使用微秒。较新的结构的timespec 使用纳秒。因此,要来回转换,你会乘以或除以1000。

On converting: struct timeval (used by gettimeofday and other older functions) uses microseconds. The newer struct timespec uses nanoseconds. So to convert back and forth, you would multiply or divide by 1000.

当计算的差别,你不用担心进位/溢出,所以你的前pression

When computing differences, you have to worry about carry/overflow, so your expression

(requestStart.tv_nsec - requestEnd.tv_nsec) / (unsigned long)1000000

是不正确的原因有两个:该差异可能走出负(即,如果时间从123.456到124.321),而你太多缩放来

is incorrect for two reasons: the difference might come out negative (that is, if the time went from 123.456 to 124.321), and you're scaling it by too much.

当我定时一些操作,我通常做的是这样的:

When I'm timing some operation, I usually do it something like this:

struct timeval before, after, elapsed;
gettimeofday(&before, NULL);
sleep(1);
gettimeofday(&after, NULL);
elapsed.tv_sec = after.tv_sec - before.tv_sec;
if(after.tv_usec >= before.tv_usec)
        elapsed.tv_usec = after.tv_usec - before.tv_usec;
else    {
        elapsed.tv_sec--;
        elapsed.tv_usec = 1000000 + after.tv_usec - before.tv_usec;
        }
printf("elapsed: %d.%06ld\n", (int)elapsed.tv_sec, (long)elapsed.tv_usec);

如果我当时就想一个结构的timespec (使用纳秒),我会乘以1000:

If I then wanted a struct timespec (using nanoseconds), I'd multiply by 1000:

struct timespec elapsed2;
elapsed2.tv_sec = elapsed.tv_sec;
elapsed2.tv_nsec = elapsed.tv_usec * 1000;
printf("elapsed: %d.%09ld\n", (int)elapsed2.tv_sec, (long)elapsed2.tv_nsec);

要从一个结构的timespec timeval结构,我会除以1000转换:

To convert from a struct timespec back to a struct timeval, I'd divide by 1000:

struct timeval elapsed3;
elapsed3.tv_sec = elapsed2.tv_sec;
elapsed3.tv_usec = elapsed2.tv_nsec / 1000;

另一个有用的就是拔出秒subseconds浮点:

Another useful thing is to pull out seconds and subseconds as floating point:

double e = (double)elapsed.tv_sec + elapsed.tv_usec / 1e6;
double e2 = (double)elapsed2.tv_sec + elapsed2.tv_nsec / 1e9;
printf("float: %f %f\n", e, e2);

(事实证明,写这类code时,根据实际类型 tv_sec tv_usec可以有奥妙 tv_nsec ,并在有用的警告你的编译器可能会选择给你看的这个问题。)

(It turns out that there can be subtleties when writing this sort of code, depending on the actual types of tv_sec and tv_usec or tv_nsec, and on the "helpful" warnings your compiler might choose to give you. See this question.)

这篇关于可移植的方法来获得时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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