Friday, July 30, 2010

Better way of using Lists in loops


Target audience: Beginners

As I always believe, we can think of in anyways to improve the performance of our application. In this post, I explain how we can use loops effectively for list iteration. In most of my projects, I have seen developers code list iteration as follows

for (int j = 0; j < aList.size(); j++) {
....
}

There is nothing wrong in this code can change this piece of code to perform well. If you see carefully, the aList.size() will be invoked every time before the next iteration of loop starts. So, what we can do is, we can assign the size of the list to a variable and use that variable inside the loop. Check the code bellow

int size = aList.size();
for (int j = 0; j < size; j++) {
....
}

This will improve the performance a bit. I can prove this by running the following benchmarking program.

package basics;

import java.util.ArrayList;
import java.util.List;

public class ListSizeExample {
public static void main(String[] args) {

long start = 0;
long end = 0;
List aList = new ArrayList();
for (int i = 0; i < 50; i++) {
aList.add(i);
}

// Using list.size() in the loop
start = System.nanoTime();
for (int i = 0; i < 100; i++) {
for (int j = 0; j < aList.size(); j++) {

}
}
end = System.nanoTime();
System.out.println("Using aList.size(): " + (end - start) / 100);

// Using size, size is assigned with value of list.size()
start = System.nanoTime();
for (int i = 0; i < 100; i++) {
int size = aList.size();
for (int j = 0; j < size; j++) {
}
}
end = System.nanoTime();
System.out.println("Using size: " + (end - start) / 100);
}
}

I have an outer loop for each benchmark code block which runs 100 times and this is just to get the average time taken for the actual loop. If you run the code, you will get an output similar to the following one

Using aList.size(): 3765
Using size: 1256

This proves that the code perform better with a assigned value of length then directly using the list.size() inside the loop

Wednesday, July 28, 2010

Improve the Application’s performance by using primitive constants


Target audience: Beginners

We use hundreds of constants in our application. It’ll be even thousands in a large scale projects. These constants are used again and again at runtime. Improving the performance of an application in any ways is advisable.So, it will be great if we can think about improving the performance while using constants.
I would suggest using primitive constants where ever it is possible than using String constants. It is advisable not only for better memory usage but also for better performance. The following benchmark program shows the difference. The most possible usage of constants is comparison. So i use comparison for benchmarking.

package basics;

public class ConstantsExample {

public static final int RED = 1;
public static final int GREEN = 2;

public static final String SRED = "RED";
public static final String SGREEN = "GREEN";

// Main methods
public static void main(String[] args) {
long start = 0;
long end = 0;
int red = 1;
String sred = "RED";

// Benchmark primitive constants
start = System.nanoTime();
for (int i = 0; i < 100; i++) {
if (RED == red) {
}
}
end = System.nanoTime();
System.out.println("Primitive comparision: "+ (end - start));

// Benchmark String constants
start = System.nanoTime();
for (int i = 0; i < 100; i++) {
if (SRED.equals(sred)) {
}
}
end = System.nanoTime();
System.out.println("String comparision: "+(end - start));
}
}

If you run this program then you will get the results similar to bellow one

Primitive comparision: 4610
String comparision: 21721

Looks good ha!

Monday, July 26, 2010

Runtime Polymorphism and Compile time Polymorphism is explained


Target audience: Beginners

Let’s look at some basics before get into details

What is polymorphism?
Polymorphism is one in many forms. That’s it. We can see lots of examples in real time. If you think about a Dog, A Dog is an Animal. A Dog can be a pet. So a Dog can be in
many forms. The Dog is Animal type and it can be another type of Pet.
Apart from types, behaviours can also take part of Polymorphism. For Example, Animals can swim. A Dog can swim, A Monkey also can swim. Dog and Monkey has their own way of swimming. Here, the swimming behavior is in many forms. A monkey can walk with two legs and also with four legs. Here, walking behaviour is in many forms. These are the examples for polymorphism in real world.

Let’s see how Polymorphism works in Java. Polymorphism allows you define a Super type and have multiple subtype implementations. There Are Two Types of Polymorphism in Java. One is compile time Polymorphism and it is sometimes referred as static Polymorphism and the other one is Runtime Polymorphism and it is sometimes referred as dynamic Polymorphism

Runtime Polymorphism
As we can have multiple subtype implementations for a super type, Java virtual machine determines the proper type to be invoked at the runtime. Method overriding is a runtime polymorphism. For example look at the following example. I have Worker interface(Super type) and have Teacher and Principal classes ( Subtypes) that implements the Worker interface .The Worker interface has a method doIt() and the subtypes implements that method.


// Worker class
interface Worker {
public void doIt();
}

// Teacher class
class Teacher implements Worker {
public void doIt() {
System.out.println("Teacher does the work");
}
}

//Principalclass
class Principal implements Worker {
public void doIt() {
System.out.println("Principal does the work");
}
}

Now I’m going to have another class which has a main method. The main method creates a List and adds a Principal instance and a Teacher instance to that list. Then it iterates through the list, refer the instances by their super type and calls the doit() method on the Super type reference

public class PolymorphismExample {

public static void main(String[] args) {
List workers = new ArrayList();

//Adding worker one
Worker worker1 = new Principal();
workers.add(worker1);

//Adding worker two
Worker worker2 = new Teacher();
workers.add(worker2);

for (Iterator iterator = workers.iterator(); iterator.hasNext();) {
Worker worker = (Worker) iterator.next();
worker.doIt();
}

}
}

If you run the PolymorphismExample class then the output will be

Principal does the work
Teacher does the work

If you see carefully, we cannot see which instance is called. We called doIt() method only on the Super type Worker but the JVM finds the proper type and called its implementation of doIt() method. This is Runtime Polymorphism. The JVM determines proper type only at runtime

Compile time Polymorphism
Method overloading is a compile time Polymorphism. As we can have multiple subtype implementations for a super type, the compiler determines which type to be invoked at the compile time. For example look at the following example. I’m going to change PolymorphismExample class to have some overloaded methods

public class PolymorphismExample {

public void doSomething(Worker worker) {
System.out.println("I'm a worker");
}

public void doSomething(Teacher teacher) {
System.out.println("I'm a Teacher");
}

public void doSomething(Principal principal) {
System.out.println("I'm a Principal");
}

public static void main(String[] args) {

PolymorphismExample example = new PolymorphismExample();
Worker principal = new Principal();
Worker teacher = new Teacher();

example.doSomething(principal);
example.doSomething(teacher);
}
}

You would expect the output as bellow, If you run the PolymorphismExample class

I'm a Principal
I'm a Teacher

WRONG, the actual output will be

I'm a worker
I'm a worker

Here the type is decided at compile time. Even though the objects are instances of Principal and Teacher, the reference is a Worker type. So the compiler picks the doSomething(Worker worker) method as it accepts the same type of reference type (Worker).
So keep it in mind when you use method overloading.