Saturday, December 8, 2018

How to reduce Execution time of Java Program?

In this post you  will learn how to reduce execution time of Java Program. The following are the some of the best practices for java programmer. 

1.Always return empty Collections and Arrays instead of null

Whenever your method is returning a collection element or an array, always make sure you return empty array/collection and not null. This will save a lot of if else testing for null elements. 
2. Avoid creating unnecessary objects and always prefer to do Lazy Initialization:

Object creation in Java is one of the most expensive operation in terms of memory utilization and performance impact. It is thus advisable to create or initialize an object only when it is required in the code.
-------------------------------------------------------------------
public class ABC {

    private List abc;
     
    public List getABC() {
         
        if(null == abc) {
            abc = new ArrayList();
        }
        return abc;
    }
}
----------------------------------------------------------------------------
3.Always try to minimize Mutability of a class:

Making a class immutable is to make it unmodified. The information the class preserve will stay as it is through out the lifetime of the class. Immutable classes are simple, they are easy to manage. They are thread safe. They makes great building blocks for other objects.
However creating immutable objects can hit performance of an app. So always choose wisely if you want your class to be immutable or not. Always try to make a small class with less fields immutable.To make a class immutable you can define its all constructors private and then create a public static method to initialize and object and return it.





4.Try to use standard library instead of writing your own from scratch

Writing code is fun. But it is very advisable to use an existing standard library which is already tested, debugged and used by others. This not only improves the efficiency of programmer but also reduces chances of adding new bugs in your code. Also using a standard library makes code readable and maintainable.

5.Wherever possible try to use Primitive types instead of Wrapper classes

Wrapper classes are great. But at same time they are slow. Primitive types are just values, whereas Wrapper classes are stores information about complete class.

6.Use Strings carefully.

Always carefully use Strings in your code. A simple concatenation of strings can reduce performance of program. For example if we concatenate strings using + operator in a for loop then every time + is used, it creates a new String object. This will affect both memory usage and performance time.

7 Algorithms Implementation
This is what you have to work on your own. There is one quote which is rarely false: "There is always a better algorithm for this task." Recursion makes your program very very slow, you can use recurrence relations to solve problems of recursion if you can't do it by some other way. Learn DP, and number theory the hard way, because for a mathematical question, you can make program fast using number theory tricks and for string/text type of questions DP gives memoized solutions.

No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...