General Java Questions I
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));
}
It looks safe, but there s a subtle flaw... 
Answer 2: Suppose you start with a vector of ten elements. On the tenth iteration i will
have the value 9 and the test i < vector.size() will succeed. 
Now suppose the thread scheduler chooses this moment to pause the current thread
and let another thread run. This second thread removes an object from the vector.
Now when the first thread resumes, the vector has only nine elements and 9 is no
file:///F|/a_jsite/350_tips/general_java I.htm (26 of 33) [2001 07 08 11:24:51]






footer




 

 

 

 

 Home | About Us | Network | Services | Support | FAQ | Control Panel | Order Online | Sitemap | Contact

java web hosting

 

Visionwebhosting.net Business web hosting division of Web Design Plus. All rights reserved