When performance is concerned, iterate a Vector or an Enumeration of said Vector?
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Travis_Jenniges
Posted On:   Thursday, March 22, 2001 09:18 AM

There is a debate going on within a software development group in which I work. The topic is whether to use a for loop like this:
for(int i=0; i < aVector.size() i++){

obj = aVector.elementAt(i);
....
}

or a while loop like this:
Enumeration enum = aVector.elements();
while(enum.hasMoreElements()){

obj = enum.nextElement();
....
}
Which way is actually more effecient? Please help us end this discussion! Thanks.

Re: When performance is concerned, iterate a Vector or an Enumeration of said Vector?

Posted By:   John_Zukowski  
Posted On:   Saturday, March 31, 2001 04:27 PM

There are some timing numbers in Practical Java. According to it....



An Enumeration is 12% faster than an Iterator/ListIterator, but a for-loop with get() is about 30% faster than that.

Also, don't get the size() each time. You only need to get it once.

What's the fastest way to traverse all the elements of a Vector? covers this in further details.

About | Sitemap | Contact