Android NDK计时器

时间:2011-08-23 18:24:38

标签: java android c java-native-interface android-ndk

我在c中编写了一段代码来计算C代码的一部分所花费的时间,然后尝试将其报告回Java代码。但问题是定时器差分总是回零。这是原生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);
}

当我运行时,我总是得到“根据difftime(),睡眠时间为-0.00000000秒”。任何想法有什么不对?

-------------------------------- 最终代码解决方案 ----- -------------------------------------------------- -

这是我发现的最终作品并不确定为什么因为我不是C大师,但无论如何它都在这里。

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

android的Java代码如下所示:

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

4 个答案:

答案 0 :(得分:7)

尝试使用gettimeofday()来衡量时间。我已成功将它与NDK一起使用,尽管在我的情况下它与pthread_cond_timedwait()一起使用。

答案 1 :(得分:2)

参见此参考资料。 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;
}

答案 2 :(得分:2)

像这样使用gettimeofday():

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

答案 3 :(得分:0)

检查sleep()的返回码以确保返回0,表示5秒已过期。也许睡眠的仿生libc实现在您的环境(模拟器/设备)中无法正常工作。或者尝试将睡眠秒数增加到60并在前后添加一些打印语句,以确保分钟处于睡眠状态。