Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / August 2006

Tip: Looking for answers? Try searching our database.

Vector efficiency

Thread view: 
Christopher Smith - 29 Aug 2006 01:35 GMT
Is there a meaningful differnece in efficiency between using a vector
<Double> and using an array double[]?
Patricia Shanahan - 29 Aug 2006 02:29 GMT
> Is there a meaningful differnece in efficiency between using a vector
> <Double> and using an array double[]?

The array is definitely more space-efficient, if you don't need the
Double objects for any other reason. It will also make more efficient
use of caches, especially for sequential scans. It may also be a bit
more time efficient, because of less indirection.

However, the real test is which works better in your program.

Patricia
Eric Sosman - 29 Aug 2006 02:31 GMT
> Is there a meaningful differnece in efficiency between using a vector
> <Double> and using an array double[]?

    Which is more efficient: A Toyota Prius or a bulldozer?
It sort of depends on the purpose to which they are put, does
it not?  A bulldozer will burn an unseemly amount of fuel if
used as a commuter vehicle, but a Prius is woefully inefficient
at digging big holes in the ground.

    Vector and array are different things, capable of different
operations.  You'll have to specify the operations you have in
mind, and what measures of "efficiency" (memory, time, ...?) are
important.

Signature

Eric Sosman
esosman@acm-dot-org.invalid

Christopher Smith - 29 Aug 2006 17:43 GMT
>      Vector and array are different things, capable of different
> operations.  You'll have to specify the operations you have in
> mind, and what measures of "efficiency" (memory, time, ...?) are
> important.

Hi Eric and Patricia --

So I did run a test between the vector approach, where I used the vector
<float>.get(i) to retrieve each record versus aligning the data into an
array declared as float[], and noticed about 2x computational speed
improvement. I guess there are a number of step performing a look up with
vector has to do as opposed to simple addressing through an array.

Chris
Tux2 - 30 Aug 2006 10:06 GMT
Even if the best collections depends on the type of operations you need
to perform, as a rule of thumb you can use :
array - for operating on fixed number of elements (you know how many
element tou will use and the number will not change in time).
java.util.List - are the best choice in case of modifiable collections
(adding, removing etc).

We need to say that java.util.Vector is only a possibile implementation
of java.util.List.

If you need synchronization (i.e. you collection is shared among
multiple threads) than you need to use java.util.Vector (and take any
other concern involved in synchronization).

If you don't need synchronization  than java.util.ArrayList ot
java.util.LinkedList may be a better choice. In particular the first is
better when tou mostly access and add elements to the end of the list,
while the second is better while removing and adding elements at random
point.

> >      Vector and array are different things, capable of different
> > operations.  You'll have to specify the operations you have in
[quoted text clipped - 10 lines]
>
> Chris
Oliver Wong - 30 Aug 2006 14:48 GMT
> Even if the best collections depends on the type of operations you need
> to perform, as a rule of thumb you can use :
[quoted text clipped - 15 lines]
> while the second is better while removing and adding elements at random
> point.

   A clarification:

ArrayList:
   Pro: Can read from random points in the array in O(1) (i.e. quickly).
   Con: Slow to add or remove elements (needs to copy the array, which is
O(n), i.e. slow)

LinkedList:
   Pro: Can add or remove elements relatively quickly into the head or tail
of the list (O(1)); adding elsewhere in this involves a seek which is
somewhat slow O(n)
   Pro: Can read from the head or tail quickly O(1), but seeking to a
random element in the middle of the list is slow (O(n)).

If you know your collection won't have duplicates and order isn't important,
or you want to use the object's natural ordering, you may consider HashSet
and TreeSet as well, which are optimized for a different set of operations.

   - Oliver
Daniel Dyer - 31 Aug 2006 00:18 GMT
> We need to say that java.util.Vector is only a possibile implementation
> of java.util.List.
>
> If you need synchronization (i.e. you collection is shared among
> multiple threads) than you need to use java.util.Vector (and take any
> other concern involved in synchronization).

That's not strictly true.  Vector pre-dates the java.util.List interface  
and just happened to have synchronised operations already.  Vector was  
modified to implement the List interface when it was introduced.  This is  
why Vector is the only core List implementation that provides  
synchronisation "out-of-the-box".  However, you can add synchronisation to  
any list implementation via the Collections.synchronizedList() method,  
which returns a synchronisation wrapper around a given list.

Dan.

Signature

Daniel Dyer
http://www.dandyer.co.uk

Eric Sosman - 30 Aug 2006 13:54 GMT
>>     Vector and array are different things, capable of different
>>operations.  You'll have to specify the operations you have in
[quoted text clipped - 8 lines]
> improvement. I guess there are a number of step performing a look up with
> vector has to do as opposed to simple addressing through an array.

    I think you're still overlooking the fact that a Vector and
an array are different things.  For example, you cannot insert
a float into a Vector.  A bit of magic called "autoboxing" takes
your primitive float value, creates a Float object from it, and
it's the Float that actually gets inserted.  It's not all *that*
expensive to create objects -- the people who write the JVM have
a strong incentive to keep the cost down -- but the amount of work
is certainly more than that involved in plunking a float value
into a float[] array.  There's also the memory issue (c.f. another
recent thread you've been involved in): The float will occupy four
bytes of memory, period, while the Float will use additional memory
to represent its "objectness" in addition to the naked value.

    That's not to say that float[] is necessarily the right answer.
Once you have created an array its size is forever fixed: you
cannot add and delete elements.  (You can change the values stored
in existing elements, but that's a different matter.)  If you make
a float[N] array and later discover that you need N+M values, your
only recourse is to create a new array and copy what's needed from
the old one.  Vector takes care of this automatically.  Even if it
does so by copying arrays behind the scenes and thus expends as much
work as if you had managed arrays manually, the convenience is not
to be sneezed at.  Expenditure of computer time is not the only
criterion of worth; one should also think about the expenditure of
programmer time.  (Which is a better use of your talents: writing
code to slosh arrays around, or thinking up creative ways to do the
analysis your problem requires?)

    Finally, micro-benchmarks can be very tricky.  Even in compiled
languages it has become harder and harder to write good ones: the
compilers' optimizers have grown more and more aggressive, and are
more apt to recognize and discard whole swathes of "useless" code.
`for (x=0; x<1000000; ++x) /* empty */ ;' may become `x = 1000000;'.
In semi-compiled, semi-interpreted, semi-JITted, garbage-collected
Java it is even more difficult to write a meaningful micro-benchmark.
Your result of "2x computational speed" is plausible, certainly, but
such measurements need to be viewed with a great deal of caution.

Signature

Eric Sosman
esosman@acm-dot-org.invalid



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.