Getting the System tick count with basic C++?

2019-02-09 06:29发布

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;
}

5条回答
Ridiculous、
2楼-- · 2019-02-09 06:36

It's not possible. The C++ standard and, as consequence the standard library, know nothing about processors or 'ticks'. This may or may not change in C++0x with the threading support but at least for now, it's not possible.

查看更多
Bombasti
3楼-- · 2019-02-09 06:41

This is platform dependent so you just have to write a wrapper and implement the specifics for each platform.

查看更多
爷的心禁止访问
4楼-- · 2019-02-09 06:42

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.

查看更多
不美不萌又怎样
5楼-- · 2019-02-09 06:45

clock() works very similarly to Windows's GetTickCount(). The units may be different. GetTickCount() returns milliseconds. clock() returns CLOCKS_PER_SEC ticks per second. Both have a max that will rollover (for Windows, that's about 49.7 days).

GetTickCount() starts at zero when the OS starts. From the docs, it looks like clock() starts when the process does. Thus you can compare times between processes with GetTickCount(), but you probably can't do that with clock().

If you're trying to compute how long something has been happening, within a single process, and you're not worried about rollover:

const clock_t start = clock();
// do stuff here
clock_t now = clock();
clock_t delta = now - start;
double seconds_elapsed = static_cast<double>(delta) / CLOCKS_PER_SEC;

Clarification: There seems to be uncertainty in whether clock() returns elapsed wall time or processor time. The first several references I checked say wall time. For example:

Returns the number of clock ticks elapsed since the program was launched.

which admittedly is a little vague. MSDN is more explicit:

The elapsed wall-clock time since the start of the process....

User darron convinced me to dig deeper, so I found a draft copy of the C standard (ISO/IEC 9899:TC2), and it says:

... returns the implementation’s best approximation to the processor time used ...

I believe every implementation I've ever used gives wall-clock time (which I suppose is an approximation to the processor time used).

Conclusion: If you're trying to time so code so you can benchmark various optimizations, then my answer is appropriate. If you're trying to implement a timeout based on actual wall-clock time, then you have to check your local implementation of clock() or use another function that is documented to give elapsed wall-clock time.

Update: With C++11, there is also the portion of the standard library, which provides a variety of clocks and types to capture times and durations. While standardized and widely available, it's not clear if the Android NDK fully supports yet.

查看更多
Rolldiameter
6楼-- · 2019-02-09 06:57

Do you have access to a vblank interrupt function (or hblank) on the Android? If so, increment a global, volatile var there for a timer.

查看更多
登录 后发表回答