如何在NDK中获取计算时间 [英] How to obtain computation time in NDK

查看:208
本文介绍了如何在NDK中获取计算时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获取使用NDK/JNI在C语言中实现的算法的某些部分的计算时间.

I need to obtain the computation time of some parts of an algorithm I have implemented in C with NDK/JNI.

我已经阅读了以下问题: Android获取当前时间戳?

I've read this question: Android Get Current timestamp?

我想我可以使用相同的方法以这种方式获得JNI调用的计算时间:

I think I can obtain the computation time of a JNI call using the same method in this way:

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

但是我需要检查本地方法中一些小部分的计算时间.我该怎么办?

But I need to check the computation times of some small parts inside the native method. How can I do it?

推荐答案

最好不要在移动设备上使用gettimeofday()currentTimeMillis().这些会返回挂钟"时间,如果网络更新了时间,则该时间可能会突然向前或向后跳.

It's best not to use gettimeofday() or currentTimeMillis() on a mobile device. These return "wall clock" time, which can jump forward or backward suddenly if the network updates the time.

使用单调时钟代替性能测量-System.nanoTime()或clock_gettime()CLOCK_MONOTONIC.请注意,这将返回struct timespec而不是struct timeval;主要区别在于时钟分辨率是纳秒而不是微秒.

Use the monotonic clock instead for performance measurements -- System.nanoTime() or clock_gettime() with CLOCK_MONOTONIC. Note this returns a struct timespec rather than a struct timeval; primary difference is that the clock resolution is nanoseconds rather than microseconds.

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

除了时钟时间之外,您可能还对每个线程的CPU时间感兴趣.参见 Android中的线程性能.

In addition to wall-clock time you may be interested in per-thread CPU time; see Thread Performance in Android.

这篇关于如何在NDK中获取计算时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆