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 / General / November 2007

Tip: Looking for answers? Try searching our database.

vector element iterator

Thread view: 
rony.john@gmail.com - 26 Nov 2007 04:13 GMT
Hi all,

    Any performance advantage for traversing elements
from list using Iterator over get(int index) ?

thanks,
ronan
Owen Jacobson - 26 Nov 2007 04:52 GMT
> Hi all,
>
>      Any performance advantage for traversing elements
> from list using Iterator over get(int index) ?

It depends on the list.

For an ArrayList or Vector[0], there isn't a noticable speed difference
between using an iterator or an index, and only a minimal (one-object)
difference in memory use.  For other List implementations that doesn't
always hold: iterating a LinkedList using get(int index) is O(n*n),
because each call to get counts from the nearest end of the list and
iterates to the target index from scratch, whereas using an iterator is
O(n), because the iterator remembers the last element you looked at for
you.

There are some clarity tradeoffs involved; explicitly using an iterator
is, in my opinion, very slightly less readable than calling get, but
implicit iterator use with the for-each syntax is much clearer than
either.

-Owen

[0] and you should really look at using ArrayList instead of Vector, if
you are using the latter; if nothing else, it doesn't impose
synchronization on you All The Time.
Mark Rafn - 26 Nov 2007 04:53 GMT
>     Any performance advantage for traversing elements
>from list using Iterator over get(int index) ?

Depends on the list implementation, but generally an iterator is as fast or
faster.  For instance, it's about the same between the two for an ArrayList,
but it'll be O(n) to use an Iterator over a LinkedList, and O(n*n) to use
get(index) for each element.

--
Mark Rafn    dagon@dagon.net    <http://www.dagon.net/>
Knute Johnson - 26 Nov 2007 05:50 GMT
>>     Any performance advantage for traversing elements
>>from list using Iterator over get(int index) ?
[quoted text clipped - 6 lines]
> --
> Mark Rafn    dagon@dagon.net    <http://www.dagon.net/>  

All of them are much slower than using an array.  If performance is
really the issue then you don't want to use any of the Collection
objects.  But usually speed isn't the most important thing.  And the
collection classes are very easy to use.

Signature

Knute Johnson
email s/nospam/knute/

Manish Pandit - 26 Nov 2007 07:16 GMT
On Nov 25, 9:50 pm, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
wrote:
> All of them are much slower than using an array.  If performance is
> really the issue then you don't want to use any of the Collection
> objects.  But usually speed isn't the most important thing.  And the
> collection classes are very easy to use.

To add to Knute's point, if you look up Vector's source code, the
get(int index) uses direct access to an array's index.

-cheers,
Manish
Mark Rafn - 27 Nov 2007 00:34 GMT
>>>     Any performance advantage for traversing elements
>>>from list using Iterator over get(int index) ?

>> Depends on the list implementation, but generally an iterator is as fast or
>> faster.  For instance, it's about the same between the two for an ArrayList,
>> but it'll be O(n) to use an Iterator over a LinkedList, and O(n*n) to use
>> get(index) for each element.

>All of them are much slower than using an array.

Hardly.  For some operations, they're massively faster. Try implementing a
queue, or anything that requires inserts and deletes from the head.  Let me
know when you're tired of copying your array...

>If performance is really the issue then you don't want to use any of the
>Collection objects.

Disagree.  Even if perf is the issue, choosing the correct data structure for
your use will outweigh the tiny (if any) advantage of arrays over array-backed
Collections.

>But usually speed isn't the most important thing.  And the collection
>classes are very easy to use.

Fully agreed.  And using an iterator rather than get(int) is generally the
right way to use a collection.  This way it works well regardless of
underlying structure.
--
Mark Rafn    dagon@dagon.net    <http://www.dagon.net/>
Roedy Green - 26 Nov 2007 06:25 GMT
On Sun, 25 Nov 2007 20:13:05 -0800 (PST), "rony.john@gmail.com"
<rony.john@gmail.com> wrote, quoted or indirectly quoted someone who
said :

>     Any performance advantage for traversing elements
>from list using Iterator over get(int index) ?

Even if there were, the use of for:each is preferred to make code
clear.
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



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.