获取系统时钟计数与基本的C ++? [英] Getting the System tick count with basic C++?

查看:289
本文介绍了获取系统时钟计数与基本的C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上是要重建的GetTickCount()函数的窗口,所以我可以在基本的C ++中使用没有任何非标准库,甚至是STL。 (因此它与Android NDK提供的库文件的规定)

我已经看过


  

时钟()


  
  

本地时间


  
  

时间


但我还是不能确定是否有可能随着时间的库复制GetTickCount的窗口功能。

任何人都可以点我在正确的方向或者即使如何做到这一点的可能吗?

什么,我想要做一个概述:

我希望能够计算出多久的申请已经做某个功能。

因此​​,例如,我希望能够计算的应用一直在努力多长时间与服务器进行注册。

我想将它移植从Windows到基于Android的linux上运行,这里是windows code:


  INT TimeoutTimer :: GetSpentTime()const的
{
如果(m_On)
{
    如果(m_Freq→1)
    {
        现在unsigned int类型;
        QueryPerformanceCounter的((INT *)及现在);
        回报(INT)((1000 *(现已M_START))/ m_Freq);
    }
    其他
    {
        回报(的GetTickCount() - (INT)M_START);
    }
}
返回-1;
}


解决方案

在Android NDK可以使用POSIX clock_gettime()调用,这是libc中的一部分。这个功能就是Android的各种计时器调用结束。

例如,java.lang.System.nanoTime()与实施

 现在结构的timespec;
clock_gettime(CLOCK_MONOTONIC,&安培;现);
回报(U8)now.tv_sec * 1000000000LL + now.tv_nsec;

本例使用单调的时钟,这是你想要的计算工期时。不像挂钟(可通过的gettimeofday()),当该装置的时钟是由网络提供商改变也不会向前或向后跳过

Linux手册页clock_gettime()描述了其它时钟可能是可用的,如每个线程经过CPU时间。

I essentially want to reconstruct the getTickCount() windows function so I can use it in basic C++ without any non standard libraries or even the STL. (So it complies with the libraries supplied with the Android NDK)

I have looked at

clock()

localtime

time

But I'm still unsure whether it is possible to replicate the getTickCount windows function with the time library.

Can anyone point me in the right direction as to how to do this or even if its possible?

An overview of what I want to do:

I want to be able to calculate how long an application has been "doing" a certain function.

So for example I want to be able to calculate how long the application has been trying to register with a server

I am trying to port it from windows to run on the linux based Android, here is the windows code:


int TimeoutTimer::GetSpentTime() const
{
if (m_On)
{
    if (m_Freq>1)
    {
        unsigned int now;
        QueryPerformanceCounter((int*)&now);
        return (int)((1000*(now-m_Start))/m_Freq);
    }
    else
    {
        return (GetTickCount()-(int)m_Start);
    }
}
return -1;
}

解决方案

On Android NDK you can use the POSIX clock_gettime() call, which is part of libc. This function is where various Android timer calls end up.

For example, java.lang.System.nanoTime() is implemented with:

struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
return (u8)now.tv_sec*1000000000LL + now.tv_nsec;

This example uses the monotonic clock, which is what you want when computing durations. Unlike the wall clock (available through gettimeofday()), it won't skip forward or backward when the device's clock is changed by the network provider.

The Linux man page for clock_gettime() describes the other clocks that may be available, such as the per-thread elapsed CPU time.

这篇关于获取系统时钟计数与基本的C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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