Measure Method Execution Time In Android Debug Build

Measure Method Execution Time In Android Debug Build

Whenever we build any Android application, then we use a number of methods for applying various features to our application. We make one method and reuse it wherever needed. These methods are responsible for increasing the users of our application by providing various extraordinary features. But at the same time, these methods can decrease the number of your users also. Why and how? If the execution of these methods is taking a very long time, then your application will give more time to the method and your application will lag or hang and this, in turn, results in disappointed users which lead to decrease in the number of users of your application.

You can improve the performance of the application by finding the execution time of the methods and then reduce the time and this, in turn, accelerates the working of your app. But how to measure the execution time of a method? Don't worry, in this blog, we will learn various methods of measuring the execution time of a method. So, let's get started by using the first and the easiest and the most effective way of finding method execution time.

Easiest method: by using Hugo

You can find the method execution time of any method my just using the Hugo library and applying the @DebugLog annotation to your method.

Read about Android Annotation Processing

All you need to do is just add the library to your project and you are good to go. Follow the below steps to add and use Hugo in your application:

  • In your project-level build.gradle file, add the below lines of code:
buildscript {
    repositories {
        google()
        jcenter()
        
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.5.0'
        classpath 'com.jakewharton.hugo:hugo-plugin:1.2.1'
    }
}
  • Add the following plugin in your module-level build.gradle file:
dependencies {
    ...
}
apply plugin: 'com.android.application'
apply plugin: 'com.jakewharton.hugo'

Now, you can use the @DebugLog annotation to your method and the details of the execution time will be logged in the build mode. The following is an example of the same:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView = findViewById(R.id.textView);
    String name = getCompanyName("MindOrks", "AfterAcademy");
    textView.setText(name);
}

// example of @DebugLog
@DebugLog
public String getCompanyName(String first, String last) {
    SystemClock.sleep(1); // Some time taking process
    return first + " " + last;
}

Apart from the method execution time, you will get to know about:

  • the parameters passed in the method
  • the return values of the method

So, in the logcat, you will see the following:

V/MainActivity: ⇢ getName(first="MindOrks", last="AfterAcademy")
V/MainActivity: ⇠ getName [1ms] = "MindOrks AfterAcademy"

The direction of the arrow will differentiate between the arguments and the return type.

You can use this in Kotlin also. All the steps are the same as that of java.

Read more about this library from here.

The traditional way of finding method execution time

If you don't want to use any third-party library to your application, then you can use the currentTimeMillis() method. Basically, the idea is to note the time when you enter into a method and also note the time when you exit from the method. At last, you can find the difference between the exit time and entry time to get the method execution time. So, we use the currentTimeMillis() method to get the current time in milliseconds. The following is an example of the same:

fun myMethod(): Long {    
    val startTime = System.currentTimeMillis()       
    ... // method execution   
    val endTime = System.currentTimeMillis()
    return endTime - startTime
}

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. So, next time whenever you build any application, then you can use these ways to find the method execution time and improve your application code accordingly.

Hope you learned something new today.

Do share this blog with your fellow developers to spread the knowledge. You can read more blogs on Android on our blogging website.

Apply Now: MindOrks Android Online Course and Learn Advanced Android

Happy Learning :)

Team MindOrks!