Use Array instead of Vector.elementAt() for Better Performance

Vector is a another legacy implementation of List interface provided with java bundle. It is almost similar to ArrayList expect it is synchronized also. It has its own advantages as well as disadvantages e.g. Vector doesn’t need additional synchronization while accessed from multiple threads but it degrades its performance for the same reason.

Here I am continuing to analyze the  performance of various java code constructs/ statements. It helps in determining the best practices in your next coding assignment.

In this post, I will compare two ways to access the elements from a Vector and see which one is more effective.

Method 1) Using method elementAt()

In this way, we will use the normal practice for accessing elements i.e. using elementsAt().

int size = v.size();
for(int i=size; i>0; i--)
{
    String str = v.elementAt(size-i);    
}

Method 2) Using array from toArray()

int size = v.size();
String vArr[] = (String[]) v.toArray();
for(int i=0; i

Comparing both methods

In below code, I am comparing both above methods for a single instance of vector class. I will iterate over all elements of vector in loop with both ways. Then we will capture the time taken in both ways of accessing the elements.

package com.howtodoinjava.demo.core.howtodoinjava;

import java.util.Calendar;
import java.util.Vector;

@SuppressWarnings("unused")
public class VectorPerformance
{
	public static void method1(Vector v)
	{
		int size = v.size();
		for(int i=size; i>0; i--)
		{
			String str = v.elementAt(size-i);
		}
	}

	public static void method2(Vector v)
	{
		int size = v.size();
		String[] vArr = new String[size];
		v.toArray(vArr);
		for(int i=0; i<size ; i++)
		{
			String str = vArr[i];
		}
	}

	public static void main(String[] args) {
		Vector<String> vector = new Vector<String>();
		for(int i=0;i<1000000;i++)
		{
			vector.add(""+i);
		}

		long startTime = Calendar.getInstance().getTimeInMillis();
		method1(vector);
		long endTime = Calendar.getInstance().getTimeInMillis();
		System.out.println("Using elementAt() :: " + (endTime - startTime) + " ms");

		startTime = Calendar.getInstance().getTimeInMillis();
		method2(vector);
		endTime = Calendar.getInstance().getTimeInMillis();
		System.out.println("Using array :: " + (endTime - startTime) + " ms");
	}
}

Output:

Using elementAt() :: 30 ms
 Using array :: 6 ms

Conclusion

As you can see that both are pretty fast for normal size collections but comparing against each other array method is way ahead in performance by 5 times. If high usage even this could be considerable. Isn’t it?

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

1 thought on “Use Array instead of Vector.elementAt() for Better Performance”

  1. Thanks for your explanation. I want to point out something in your code. Please correct me if I am wrong. It is better to System.currentTimeMillis() rather than Calendar.getInstance().getTimeInMillis(). Calendar object is considered to be the heavy object. Though it is out of context but just wanted point it out. Thank you.

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.