Open In App

How to Measure Method Execution Time in Android Debug Build?

Improve
Improve
Like Article
Like
Save
Share
Report

We use a variety of approaches to apply certain features to our Android applications whenever we create them. We create one method and apply it to all of our projects. These strategies are responsible for growing the number of people who use our software by delivering a variety of unique features. However, these tactics can also reduce the number of users on your site. Why and how did this happen? If these methods take a long time to execute, your application will allocate extra time to them, causing your app to lag or hang, resulting in dissatisfied consumers and a decline in the number of people that use your app.

You may increase the performance of your app by determining the execution time of the methods and then reducing that time, which will speed up the operation of your app. But how do you calculate a method’s execution time? Don’t worry, we’ll cover a variety of approaches for determining a method’s execution time in this article. So, let’s get started with the first, simplest, and most effective technique of determining method execution time.

Using Hugo, The Simplest Way!

You may get the method execution time of any method by using the Hugo library and annotating your method with the @DebugLog annotation. Check out this article on Android Annotation Processing. You simply need to include the library in your project and you’re ready to go. To add and use Hugo in your application, follow the steps below:

Step 1: The Following lines in your grade build will do the needful

buildscript {
   repositories {
       google()
       jcenter()
       
   }
   dependencies {
       classpath 'com.android.tools.build:gradle:3.5.0'
       classpath 'com.jakewharton.hugo:hugo-plugin:1.2.1'
   }
}

 Step 2: Adding the required plugin

implementations {
   ...
}
apply plugin: 'com.android.application'
apply plugin: 'com.jakewharton.hugo'

Java




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView = findViewById(R.id.textView);
    String name = getAboutTheAuthor("Geeks for Geeks", "Android Articles by Spandan");
    textView.setText(name);
}
  
// You will now override this 
// method, in the following method:
@DebugLog
public String getAboutTheAuthor(String first, String last) {
      // Assigning the time for the process to sleep and then
      // partially waking the system up, the time is in millisecond.
      // In this condition it is about five milliseconds which is
      // almost negligible to the normal human brain.
    SystemClock.sleep(5); 
    return hello + " " + last;
}


In addition to the method execution time, you will learn about the parameters that the method accepts the method’s returned values

As a result, you’ll notice the following in the logcat:

V/MainActivity: ⇢ getName(first="GeeksforGeeks", last="Android Articles by Spandan")
V/MainActivity: ⇠ getName [1ms] = "geeks for geeks articles"

The arrow’s orientation will distinguish between the arguments and the return type. You may also use this in Kotlin. All of the stages are identical to those in Java.


Last Updated : 15 Feb, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads