如何从一个32位int重新presenting时间转换成微秒为32位int重新presenting时间为秒二进制小数? [英] How do I convert from a 32-bit int representing time in usec to a 32-bit int representing time as a binary fraction in secs?

查看:132
本文介绍了如何从一个32位int重新presenting时间转换成微秒为32位int重新presenting时间为秒二进制小数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

POSIX使用 timeval结构重新present的时间间隔。

POSIX uses struct timeval to represent time intervals.

struct timeval
{
    time_t   tv_sec;
    unsigned tv_usec;
};

GHS完整性重新presents 时间按以下方式,

struct Time
{
    time_t Seconds;
    unsigned Fraction;
};

例如,0.5秒是psented为为0x80000000 重新$ P $和0.25sec被psented为为0x40000000

For example, 0.5sec is represented as 0x80000000 and 0.25sec is represented as 0x40000000.

什么是从的timeval 转换为时间

(P.S,答案是不会的POSIX库链接到诚信和使用POSIX调用。)

(p.s. The answer is not to link the POSIX library into Integrity and use POSIX calls.)

推荐答案

这是重新present时间不寻常的方式。

This is an unusual way to represent time.

不管怎样,有两种简单的方法来做到这一点无论哪种方式,如果你有64位整数或浮动点(前者更有可能在嵌入式系统上):

Anyway, there are two easy ways to do it either way if you have 64-bit integers or floating points (the former are more likely on an embedded system):

/* assuming long is 64-bit and int is 32-bit
   or in general long twice the size of int: */
Fraction = (long) tv_usec * UINT_MAX / 1000000        /* usecs to fraction */
tv_usec = (long) Fraction * 1000000 / UINT_MAX        /* fraction to usecs */

/* assuming floating points are available: */
Fraction = tv_usec * ((double) UINT_MAX / 1000000)    /* usecs to fraction */
tv_usec = Fraction * ((double) 1000000 / UINT_MAX)    /* fraction to usecs */

显然都是唯一整数近似值,因为在一个尺度最值不能被重新psented如在其它尺度整数$ P $。而在一个方向,你可能会失去一些precision因为分数表格可以重新present更精细倍 - 一个增量分数表格小于0.00024微秒。不过,这只是如果你的计时器居然能衡量这些价值观是不太可能 - 大部分计时器甚至不能测量在微秒的规模,你在 tv_usec 看值常为圆形。

Obviously both are only integer approximations, because most values in one scale cannot be represented as integers in the other scale. And in one direction you may be losing some precision because the Fraction form can represent much finer times - one increment of the Fraction form is less than 0.00024 microseconds. But that is only if your timer can actually measure those values which is not very likely - most timers cannot even measure at the scale of microseconds, and the value you see in tv_usec is often rounded.

如果没有64位整数浮点也不点是提供一种选择,你可以使用一个额外的变量做迭代。我在想,如果有一个更简单的(和更便宜,考虑到这是计时code)的方式做这样的缩放比使用两个32位整数做迭代64位乘法和除法的等价物。那来到我的脑海两个想法,一是不会做精确缩放,甚至可以和产生的结果是多达9位了,而且弥补了原来不被任何便宜的人。如果新的东西在我脑海中出现,我将它张贴在这里,但是这是一个有趣的挑战。没有任何人有一个良好的算法或片段?也许一个小precomputed表的帮助?

If neither 64-bit integers nor floating points are available an option, you could do it iteratively with an extra variable. I was thinking if there is a simpler (and less expensive, considering that this is timing code) way to do such scaling than doing the equivalent of iterative 64-bit multiplication and division with two 32-bit integers. Of the two ideas that came to my mind, one would not do exact even scaling and may produce results that are by up to 9 bits off, and the one that compensates for that turns out not to be any cheaper. If something new comes up in my mind I will post it here, but this is an interesting challenge. Does anyone else have a good algorithm or snippet? Perhaps with the aid of a small precomputed table?

这篇关于如何从一个32位int重新presenting时间转换成微秒为32位int重新presenting时间为秒二进制小数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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