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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

Q: What is better to use: array or vector?<br />

Just wondering as I am using Vectors to store large amounts of objects from 50 to<br />

4000 and each one has to be "looked at" every time paint is called...<br />

Just wondering if it would be better to use an array, list etc?<br />

Answer 1: Since the Vector method uses an array for storage but has extra steps<br />

involved in getting an element, use an array for fastest access.<br />

--<br />

WBB <strong>Java</strong> Cert mock exams http://www.lanw.com/java/javacert/<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 vector<br />

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

space than the current size, you have to hardcode their copying into a 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 a<br />

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

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

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 />

file:///F|/350_t/350_tips/general_java-I.htm (24 of 31) [2002-02-27 21:18:17]

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

Saved successfully!

Ooh no, something went wrong!