Question

I am somewhat new to Java/Android programming and I am just trying to do a simple time measure to calculate the elapsed time of a method that I am calling.

Right now my code looks like this:

        startTime = System.currentTimeMillis();
        java_doubleinterp(args, ans);
        endTime = System.currentTimeMillis();
        elapsed = endTime - startTime;
        setTextview(ans);

The method java_doubleinterp contains a for loop and it is purely mathematical and working properly. The input is args and the output is ans. I am not returning anything because I am trying to compare the performance of this to a similar function implemented using C/C++ (NDK) that was generated automatically with MatLab and has this structure.

For the C/C++ function, I am trying to measure the time in the same way and getting the same error.

Later in the code I have this:

Toast elapsedTime = Toast.makeText(getApplicationContext(), 
            "Elapsed time for " + lang + " code: \n" + (elapsed*1000)+ "\u03BCs", 
            Toast.LENGTH_LONG);

where lang is just a string.

This toast is returning me that the elapsed time is 0us (microseconds).

I displayed the startTime and endTime when trying to debug this problem and both of them show the same value indeed. Why is this happening?


Obs: I tried using nanoTime() to get the times and have better precision but the results were:

  • Emulator: around 50us
  • Phone (Nexus 4) : either 0us or 30us [doesn't make sense]

EDIT:

I made some changes in my code and it is looking like this now for the java method:

startTime = System.nanoTime();
java_doubleinterp(args, ans);
setTextview(ans);
endTime = System.nanoTime();
elapsed = endTime - startTime;

And this is the part for the c function (JNI):

startTime = System.nanoTime();
doubleinterp(args, ans);
setTextview(ans);
endTime = System.nanoTime();
elapsed = endTime - startTime;

They are now working fine. Since the setTextview is the same method for the two parts of code, the absolute execution time may be wrong but the relative execution time (between java implementation and JNI implementation) is ok.

Now I am getting average execution times that make more sense:

  • Emulator: 3.75 ms (Java) / 6.59 ms (JNI)
  • Nexus 4: 1.11ms (Java) / 0.82 ms (JNI)
Était-ce utile?

La solution

You're trying to measure the performance of a dummy routine: odds are that the compiler is simply making the entire thing go away, which is why you're getting 0 microseconds.

Autres conseils

Make sure that variables startTime and endTime are Long

It seems that your routine finishes very fast so that the difference between your start time and end is 0. When we measure the performance, as far as I know, nanoTime() is much better than currentTimeMillis(). Try to increase the size of input

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top