27.04.2013 Views

330 Java Tips.pdf - FTP Server

330 Java Tips.pdf - FTP Server

330 Java Tips.pdf - FTP Server

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

General <strong>Java</strong> Questions IV<br />

Answer 2: arrays are faster, vectors are more dynamic.<br />

This should be evident just looking at the amount of code you need to traverse one<br />

versus the other. It might also be beneficial to write a linkedlist class and use that.<br />

That way you have a dynamic container which has potential to be faster than a<br />

vector (though still not as fast as an array). The problem with arrays is that if you<br />

need more space than the current size, you have to hardcode their copying into a<br />

bigger array.<br />

Conversely, if you never (or rarely) use the entire array, its a waste of space and<br />

memory.<br />

The following are benchmark test results of vector vs. array (ran on a 200-Mhz<br />

Pentium w/ 96 Mbytes of memory and Windows95 ):<br />

Allocating vector elements: 17910 milliseconds<br />

Allocating array elements: 4220 milliseconds<br />

Accessing Vector elements: 18130 milliseconds<br />

Accessing array elements: 10110 milliseconds<br />

One other reason for vectors being slower that I did not mention above is that vector<br />

methods are synchronized, which creates a performance bottleneck.<br />

Hope this helps<br />

--<br />

MSW<br />

Q: Would anyone know the performance issues regarding Vector's?<br />

I am actually talking about resource pooling. I have objects that wait in a queue. It is<br />

a vector that keeps growing, as the queue gets bigger.<br />

Do Vectors have much performance hit? Is there a better way to implement vectors<br />

to get the best out of them? Or am I better of creating a fixed size array?<br />

Answer 1:<br />

If you just want a LIFO or LILO queue, you may be better off with<br />

LinkedList than with Vector, as then you'll never have to wait for the contents to be<br />

copied.<br />

Vectors perform pretty well, but if you know (even roughly) how big you're going to<br />

need it to be, specifying that in the constructor call can help.<br />

How sure are you that this will be a performance bottleneck? Premature optimisation<br />

is the root of all evil...<br />

The Vector class is thread-safe. By that I mean that there is no way to corrupt the<br />

internal representation of the data by accessing the vector from more than one<br />

thread. However, it is still possible, very easy in fact, to use a vector in a way that is<br />

not thread safe.<br />

Consider this code:<br />

for (int i = 0; i < vector.size(); i++) {<br />

System.out.println(vector.elementAt(i));<br />

}<br />

file:///F|/350_t/350_tips/general_java-IV.htm (3 of 10) [2002-02-27 21:18:34]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!