Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the equivalent of System.currenttimemilliseconds in NDK? [duplicate]

Tags:

android-ndk

I was wondering if there is an easy way to get the current time in native Android code. Optimally it would be something comparable to System.getTimeMillies(). I will only be using it to see how long certain function calls will take so a long variable with the current time in milliseconds would be the optimal solution for me.

Thanks in advance!

like image 206
Pandoro Avatar asked Nov 25 '22 04:11

Pandoro


1 Answers

For the lazy, add this to the top of your code:

#include <time.h>

// from android samples
/* return current time in milliseconds */
static double now_ms(void) {

    struct timespec res;
    clock_gettime(CLOCK_REALTIME, &res);
    return 1000.0 * res.tv_sec + (double) res.tv_nsec / 1e6;

}

Call it like this:

double start = now_ms(); // start time

// YOUR CODE HERE

double end = now_ms(); // finish time

double delta = end - start; // time your code took to exec in ms
like image 106
torger Avatar answered May 19 '23 06:05

torger