Search code examples
androidprofilingjava-native-interface

High-precision timer in Android JNI


I'm trying to profile my JNI application. Is there something like "Get ticks since startup" that I can use to measure functions and/or systems? Anything with the precision of 1/10 of a millisecond will do.

Obviously, a fully native function would be nicer, I'd prefer not to call a Java function for every single thing I'm trying to profile, but if that's the only option, I'll take that too.


Solution

  • clock_gettime(). You'll see sample code in android-ndk-r5b/samples/hello-neon/jni/helloneon.c and android-ndk-r5b/samples/native-plasma/jni/plasma.c.

    You want to look for something like CLOCK_MONOTONIC to make sure you're getting ticks since startup, not the wall clock.