Skip to content
Related Articles

Related Articles

Improve Article

How Much Time it Takes For an Android Debug Build?

  • Last Updated : 25 Aug, 2021
Geek Week

When we create an Android application, we employ a variety of techniques for incorporating certain functionalities. We create one method and utilize it everywhere it is required. These approaches are responsible for boosting the number of users of our program by delivering a variety of exceptional features. However, these approaches may potentially reduce the number of your consumers. Why and how is this so? If the execution of these methods takes a long time, your application will give the method additional time and your application may slow or hang, resulting in unhappy consumers and a drop in the number of users of your application.

You may enhance the application’s performance by determining the execution time of the methods and then reducing the duration, which in turn speeds up the app’s operation. But how do you calculate a method’s execution time? Here, we will learn about several ways for assessing a method’s execution time. So, let’s get started by employing the first, simplest, and most effective technique of determining method execution time.

The Simplest Way is to Use Hugo

You can determine the method execution time of any method by simply utilizing the Hugo library and annotating it with the @DebugLog tag. Read more about Android Annotation Processing 

Adding Hugo Library

To add and utilize Hugo in your application, follow the steps below: Add the following lines of code to your project’s build.gradle file.



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

Then add the below plugin in your app gradle file:

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

You may now add the @DebugLog annotation to your method, and the execution time will be reported in the build mode. As an example, consider the following:

Java




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gfgActivity);
    gfgViewText = findViewById(R.id.gfgTextView);
    String name = getCompanyName("GeeksforGeeks", "Spandan Saxena");
    gfgViewText.setText(name);
}
@DebugLog
public String getCompanyName(String start, String end) {
    SystemClock.sleep(5); // Some Process
    return first + " " + last;
}

Aside from the method execution time, you will learn about: the arguments given in the method return values So, in the logcat, you will notice the following:

Kotlin




V/MainActivity: ⇢ getName(start="GeeksforGeeks", end="Spandan Saxena")
V/MainActivity: ⇠ getName [1ms] = "GeeksforGeeks Spandan Saxena"

The arrow’s direction distinguishes between the parameters and the return type.

GeekTip: This is also applicable in Kotlin. All of the stages are the same as in Java.

The Conventional Method of Determining Method Execution Time

If you do not wish to include a third-party library in your application, you can use the currentTimeMillis() function. Essentially, the goal is to record the time you enter a method as well as the time you depart the method. Finally, you can calculate the method execution time by subtracting the exit time from the entrance time. So, to retrieve the current time in milliseconds, we utilize the currentTimeMillis() function. As an example, consider the following:

Kotlin




fun gfgMethod(): Long {
    // some methods
    val beginTime = System.currentTimeMillis()        
    val endingTime = System.currentTimeMillis()
    return endingTime - beginTime
}

GeekTip: To retrieve the time in nanoseconds, use the nanoTime() function.

Conclusion

These are several methods for determining the method execution time of any method. So, the next time you develop an application, you may utilize these methods to determine the method execution time and update your application code accordingly.

Want a more fast-paced & competitive environment to learn the fundamentals of Android?
Click here to head to a guide uniquely curated by our experts with the aim to make you industry ready in no time!



My Personal Notes arrow_drop_up
Recommended Articles
Page :