General Java Questions IV
Answer 2: arrays are faster, vectors are more dynamic.
This should be evident just looking at the amount of code you need to traverse one
versus the other. It might also be beneficial to write a linkedlist class and use that.
That way you have a dynamic container which has potential to be faster than a
vector (though still not as fast as an array). The problem with arrays is that if you
need more space than the current size, you have to hardcode their copying into a
bigger array.
Conversely, if you never (or rarely) use the entire array, its a waste of space and
memory.
The following are benchmark test results of vector vs. array (ran on a 200 Mhz
Pentium w/ 96 Mbytes of memory and Windows95 ):
Allocating vector elements: 17910 milliseconds
Allocating array elements: 4220 milliseconds
Accessing Vector elements: 18130 milliseconds
Accessing array elements: 10110 milliseconds
One other reason for vectors being slower that I did not mention above is that vector
methods are synchronized, which creates a performance bottleneck.
Hope this helps
MSW
Q: Would anyone know the performance issues regarding Vector s?
I am actually talking about resource pooling. I have objects that wait in a queue. It is
a vector that keeps growing, as the queue gets bigger.
Do Vectors have much performance hit? Is there a better way to implement vectors
to get the best out of them? Or am I better of creating a fixed size array?
Answer 1:
If you just want a LIFO or LILO queue, you may be better off with
LinkedList than with Vector, as then you ll never have to wait for the contents to be
copied.
Vectors perform pretty well, but if you know (even roughly) how big you re going to
need it to be, specifying that in the constructor call can help.
How sure are you that this will be a performance bottleneck? Premature optimisation
is the root of all evil...
The Vector class is thread safe. By that I mean that there is no way to corrupt the
internal representation of the data by accessing the vector from more than one
thread. However, it is still possible, very easy in fact, to use a vector in a way that is
not thread safe.
Consider this code:
for (int i = 0; i < vector.size(); i++) {
System.out.println(vector.elementAt(i));
}
file:///F|/a_jsite/350_tips/general_java IV.htm (3 of 10) [2001 07 08 11:24:54]
footer
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved