Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test function for execution timing in android?

I have got stucked where one of my function is taking some time to execute. I have a hierarchy of objects in object using object models and ArrayList(s). I just want to know the techniques by which I can debug the code to check which statement of code is taking time in execution.

like image 701
Aashutosh Sharma Avatar asked Nov 14 '14 08:11

Aashutosh Sharma


People also ask

How to measure method execution time in Android?

You can use the nanoTime() method to get the time in nanoseconds. These are some of the ways of finding the method execution time of any method.

How is program execution time calculated?

The CPU time is represented by the data type clock_t, which is the number of ticks of the clock signal. By dividing the number of clock cycles by the clocks per seconds, you have the total amount of time a process has been actively using a CPU after an arbitrary event.

How do you time how long a function takes in Java?

The currentTimeMillis() method returns the current time in milliseconds. To find the elapsed time for a method you can get the difference between time values before and after the execution of the desired method. The nanoTime() method returns the current time in nano seconds.


2 Answers

Simply use this.

long startTime = System.nanoTime();
YourMethode();
long endTime = System.nanoTime();

long MethodeDuration = (endTime - startTime);
like image 54
Remees M Syde Avatar answered Sep 24 '22 19:09

Remees M Syde


Two solutions :

  • Display yourself the time elapsed (pretty simple and extremely powerful imho) : How do I time a method's execution in Java?
  • Get more details with Traceview : http://developer.android.com/tools/debugging/systrace.html
like image 27
mithrop Avatar answered Sep 22 '22 19:09

mithrop