2011-08-23 9 views
10

Napisałem fragment kodu c, aby obliczyć, ile czasu zajmuje sekcja kodu C, a następnie próbuję zgłosić go z powrotem do kodu Java. Problem polega jednak na tym, że różnica czasu zawsze powraca jako zero. tutaj jest natywny CLiczniki czasu Android NDK

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

Gdy uruchomię to ja zawsze „według difftime(), spał na -0.00000000 sekund”. Jakieś pomysły, co jest nie tak?

-------------------------------- Ostateczny kod Rozwiązanie --------- -----------------------------------------------

To właśnie znalazłem w końcu nie wiem dlaczego, ponieważ nie jestem guru C, ale tutaj jest tak.

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

kod Java dla android wygląda następująco:

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

Tylko obsługiwać pytania użycia tutaj, pytania dev dalej [SO]. –

Odpowiedz

7

Spróbuj użyć gettimeofday() do pomiaru czasu. Z powodzeniem użyłem go w NDK, chociaż w moim przypadku było to z pthread_cond_timedwait().

2

Zobacz to odwołanie. 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

Już uruchomiłem tę wersję kodu przy użyciu timerów i nadal mam 0 sekund. Dlatego poszedłem inną drogą z powyższym kodem. Musi być coś z Natywnymi połączeniami i zegarem systemowym. – JPM

+0

Czy próbowałeś użyć Javy do pomiaru czasu? – Frohnzie

0

Sprawdź kod powrotu do snu(), aby upewnić się, że zwracane jest 0, co oznacza, że ​​5 sekund wygasły. Być może implementacja bionicznego libc snu nie działa poprawnie w twoim środowisku (emulator/urządzenie). Lub spróbuj zwiększyć liczbę sekund snu do 60 i dodaj kilka instrukcji drukowania przed i po, aby upewnić się, że minuta jest uśpiona.

2

Zastosowanie gettimeofday() tak:

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