Android NDK定时器

10

我使用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);
}

运行这个程序时,我总是会得到"according to difftime(), slept for -0.00000000 seconds"的输出。有任何想法出了什么问题吗?

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

这是我最终发现的可行代码,虽然我不是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();
    }
}

1
我们这里只处理使用问题,开发问题请到[SO]。 - Matthew Read
4个回答

7
尝试使用gettimeofday()来测量时间。我曾在NDK中成功地将其与pthread_cond_timedwait()一起使用,尽管这是针对我的情况的。

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

我已经使用计时器运行了此代码版本,仍然得到了0秒的结果。这就是为什么我使用了上面的不同代码路线。这可能与本机调用和系统时钟有关。 - JPM

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

0

检查sleep()的返回代码,确保返回0,表示5秒已经过去。也许在您的环境(模拟器/设备)中,bionic libc实现的sleep不正常工作。或者尝试将睡眠秒数增加到60,并在之前和之后添加一些打印语句,以确保发生了一分钟的睡眠。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接