views:

903

answers:

5

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;
}
+2  A: 

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.

Noah Roberts
+1  A: 

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.

Michael Dorgan
+3  A: 

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

daramarak
On Android only the very basic C libraries are provided, what you see under c library here - http://www.cplusplus.com/reference/clibrary/ , it has no additional platform specfic extras
Donal Rafferty
The C library do have the time.h isn't that available? It might not give you great resolution of time, but should be enough for your needs. use clock() with the CLOCKS_PER_SEC to get elapsed time.
daramarak
+2  A: 

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 about 49.7 days).

GetTickCount() starts at zero when the PC is booted. 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;
if (seconds_elapsed > timeout) { ... }
Adrian McCarthy
+3  A: 

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.

fadden