Android NDK timers

12,623

Solution 1

Try using gettimeofday() to measure time. I have successfully used it with the NDK, although in my case it was with pthread_cond_timedwait().

Solution 2

See this reference. http://www.cplusplus.com/reference/clibrary/ctime/difftime/

/* difftime example */
#include <stdio.h>
#include <time.h>

int main ()
{
  time_t start,end;
  char szInput [256];
  double dif;

  time (&start);
  printf ("Please, enter your name: ");
  gets (szInput);
  time (&end);
  dif = difftime (end,start);
  printf ("Hi %s.\n", szInput);
  printf ("It took you %.2lf seconds to type your name.\n", dif );

  return 0;
}

Solution 3

Use gettimeofday() like this:

bool QueryPerformanceCounter( int64_t* performance_count )
{
    struct timeval Time;

    /* Grab the current time. */
    gettimeofday( &Time, NULL );
    *performance_count = Time.tv_usec + /* Microseconds. */
                         Time.tv_sec * usec_per_sec; /* Seconds. */

    return true;
}
Share:
12,623
JPM
Author by

JPM

Java Programmer for 6 years and seasoned Android developer, some C#, slightly new Windows Mobile developer. Member of the Bluetooth SIG Naming and Version Committee Musically inclined, play guitar and singer/songwriter, band investor among many other titles in the music biz. I am nerdier than 95% of all people. Are you a nerd? Click here to take the Nerd Test, get nerdy images and jokes, and talk on the nerd forum! http://www.nerdtests.com/images/ft/nq/a287f5ee0b.gif

Updated on July 19, 2022

Comments

  • JPM
    JPM almost 2 years

    I wrote a piece of code in c to calculate how long a section of the C code was taking, then trying to report it back to the Java code. But the problem is that the timer differential always comes back as zero. here is the native C

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h> /* sleep() */
    #include <time.h>
    #include <jni.h>
    
    jstring Java_com_nsf_ndkfoo_NDKFooActivity_invokeNativeFunction(JNIEnv* env, jobject javaThis) {
    
        time_t start, end;
    
        start = time(NULL);
        if(start == (time_t)-1) {
          return 1;
        }
    
        sleep(5);
    
        end = time(NULL);
    
        char buf[60] = { 0 };
    
        sprintf(buf,"according to difftime(), slept for %.8f seconds\n", (int)difftime(end, start));
    
        return (*env)->NewStringUTF(env, buf);
    }
    

    When I run this I always get "according to difftime(), slept for -0.00000000 seconds". Any ideas what's wrong?

    --------------------------------Final Code Solution--------------------------------------------------------

    This is what I found finally works not sure why as I am not a C guru but here it is anyway.

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h> /* sleep() */
    #include <sys/time.h>
    #include <jni.h>
    
    jstring Java_com_nsf_ndkfoo_NDKFooActivity_invokeNativeFunction(JNIEnv* env, jobject javaThis) {
    
        struct timeval start;
        struct timeval end;
    
        gettimeofday(&start, NULL);
        sleep(5);
    
        gettimeofday(&end, NULL);
    
        char buf[60] = { 0 };
    
        sprintf(buf,"according to difftime(), slept for %ld seconds\n",  ((end.tv_sec * 1000000 + end.tv_usec) - (start.tv_sec * 1000000 + start.tv_usec)));
    
        return (*env)->NewStringUTF(env, buf);
    }
    

    Java code for android looks like this:

    package com.nsf.ndkfoo;
    
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.os.Bundle;
    
    public class NDKFooActivity extends Activity {
        // load the library - name matches jni/Android.mk
        static {
            System.loadLibrary("ndkfoo");
        }
    
        // declare the native code function - must match ndkfoo.c
        private native String invokeNativeFunction();
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            // this is where we call the native code
            String hello = invokeNativeFunction();
    
            new AlertDialog.Builder(this).setMessage(hello).show();
        }
    }
    
  • JPM
    JPM over 12 years
    I already ran this version of the code using timers and still got 0 secs. That is why I went a different route with the code above. Must be something with Native calls and system clock.