11

NDK/JNI を使用して C で実装したアルゴリズムの一部の計算時間を取得する必要があります。

この質問を読みました: Android Get Current timestamp?

この方法で同じ方法を使用して、JNI 呼び出しの計算時間を取得できると思います。

Long start, end, time;
start = System.currentTimeMillis()/1000;
//my native call
end = System.currentTimeMillis()/1000;
time = end - start;
Log.i(TAG, "Time ... (ms): " + time);

しかし、ネイティブ メソッド内のいくつかの小さな部分の計算時間を確認する必要があります。どうすればいいですか?

4

2 に答える 2

16

gettimeofday()またはcurrentTimeMillis()モバイル デバイスでは使用しないことをお勧めします。これらは「壁時計」時間を返します。ネットワークが時間を更新すると、突然前後にジャンプする可能性があります。

代わりに単調クロックをパフォーマンス測定に使用します -- System.nanoTime() またはclock_gettime()とともにCLOCK_MONOTONICstruct timespecこれはではなく を返すことに注意してくださいstruct timeval。主な違いは、クロックの分解能がマイクロ秒ではなくナノ秒であることです。

int64_t getTimeNsec() {
    struct timespec now;
    clock_gettime(CLOCK_MONOTONIC, &now);
    return (int64_t) now.tv_sec*1000000000LL + now.tv_nsec;
}

実時間に加えて、スレッドごとの CPU 時間にも関心があるかもしれません。Android でのスレッドのパフォーマンス を参照してください。

于 2013-06-20T14:47:54.393 に答える
7

From within your C/C++ code,

#include <sys/time.h>
long long currentTimeInMilliseconds()
{
    struct timeval tv;
    gettimeofday(&tv, NULL);
    return ((tv.tv_sec * 1000) + (tv.tv_usec / 1000));
}

This will get you a structure with the current time in seconds and microseconds, giving you enough to measure time between two points fairly easily. It then performs the conversion to return the current time, in milliseconds.

Edit: updated per @ChrisStratton's suggestion.

于 2013-06-19T18:59:02.487 に答える