Windows下面获取64位的1ms授时(timeGetTime64)

本文介绍了如何在Windows下通过结合GetTickCount64和timeGetTime函数,解决时间精度问题,确保高精度时间测量,尤其在长周期内保持准确性。作者针对时间重置限制提出了跨函数融合的解决方案。
摘要由CSDN通过智能技术生成
                U64 result = 0;
        
                #ifdef _WIN32
                
                U64 tick = GetTickCount64();    // 10-16ms tolerance
                U64 time = timeGetTime();       // 1ms tolerance, but 49.5 days limit.

                // bias 65536ms [0-65535], this vlaue must be far greater then tick and time's diff.
                // if equal, return directly.
                result = ( tick & 0xFFFFFFFFFFFF0000 ) | ( time & 0x000000000000FFFF );

                // compare result with tick
                if( result > tick )
                {
                    // clamp tick to U32_MAX range;
                    U64 diff = tick & U32_MAX;
                    
                    // need carry ? 
                    if( tick  > U32_MAX ) {
                        diff |= 0x100000000;
                    }

                    // this vlaue usualy less then 16 ms.
                    diff > time ? diff = diff - time : diff = time - diff;

                    // if diff out of U32_MAX range, clamp it.
                    if( diff  > U32_MAX ) {
                        diff &= U32_MAX;
                    }

                    // if diff out of U16_MAX range, reverse bits.
                    if( diff  > U16_MAX ) 
                    {
                        if( diff > U32(~diff) ) {
                            diff = U32(~diff) + U64(1);
                        }
                    }

                    // this value usualy equal to diff.
                    if( result - tick > diff )
                    {
                        result -= 0x10000;
                    }
                }
                else 
                if( result < tick )
                {
                    U64 diff = tick & U32_MAX;
                    
                    if( tick  > U32_MAX ) {
                        diff |= 0x100000000;
                    }

                    // this vlaue usualy less then 16 ms.
                    diff > time ? diff = diff - time : diff = time - diff;

                    if( diff  > U32_MAX ) {
                        diff &= U32_MAX;
                    }

                    if( diff  > U16_MAX ) 
                    {
                        if( diff > U32(~diff) ) {
                            diff = U32(~diff);
                        }
                    }

                    // this value usualy equal to diff.
                    if( tick - result > diff )
                    {
                        result += 0x10000;
                    }
                }

                #endif
        
                return result;

多的不说,直接上代码。。。

Windows下面是没有timeGetTime64这个函数的。因此,timeGetTime返回值(32位)会在49.5天内重置。这样可能会导致一些问题。

想了一个办法,用Windows的另外一个函数GetTickCount64来获取大致时间,这个时间有10-16ms的误差,然后再用timeGetTime获取高精度的授时。然后组合一下。就可以形成U64位的高精度返回值。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值