【问题标题】:Android NDK timersAndroid NDK 计时器
【发布时间】:2011-11-02 05:05:09
【问题描述】:

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

【问题讨论】:

  • 我们这里只处理使用问题,开发问题继续Stack Overflow

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


【解决方案1】:

像这样使用 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;
}

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        请参阅此参考。 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 秒。这就是为什么我用上面的代码走了一条不同的路线。必须是带有本地调用和系统时钟的东西。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-05-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多