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

Tip: Looking for answers? Try searching our database.

List to Array speed

Thread view: 
Peter Plumber - 14 Jun 2006 13:17 GMT
Hi,

just for interest:
is there any speed difference between those lines?

    String[] names = (String[])party.toArray(new String[0]);
    String[] names = (String[])party.toArray(new String[party.size()]);

thx

Peter

PS: are there any classes or tools for speed measuring?
Peter Van Weert - 14 Jun 2006 13:30 GMT
I suppose you are talking about the toArray method defined in the
java.util.Collection interface. Read the specification and you'll know
your answer; I quote:

"Returns an array containing all of the elements in this list in the
correct order; the runtime type of the returned array is that of the
specified array.  If the list fits in the specified array, it is
returned therein.  Otherwise, a new array is allocated with the runtime
type of the specified array and the size of this list."

So if party.size() > 0 (and the implementation obeys the contract), your
first line will be slower because you need to allocate two arrays. In
fact, an implementation will be forced to use reflection to create a new
instance (generic Array creation...), so allocating it will become quite
expensive. As an example, have a look at Sun's reference implementation
in java.util.ArrayList:

public <T> T[] toArray(T[] a) {
       if (a.length < size)
           a = (T[])java.lang.reflect.Array.
        newInstance(a.getClass().getComponentType(), size);
    System.arraycopy(elementData, 0, a, 0, size);
       if (a.length > size)
           a[size] = null;
    return a;
}

Cheers,
Peter

ps: love you forename btw!

> Hi,
>
[quoted text clipped - 9 lines]
>
> PS: are there any classes or tools for speed measuring?
Peter Van Weert - 14 Jun 2006 14:09 GMT
After reading Eric Sosman's mail, I see that I indeed forgot to take
into account the size()-method call, which can take arbitrary long. As
always, everything depends on the concrete implementation of both
methods (size and toArray), and it is impossible to say which of both
options will be most efficient unless you know which class party has.
I guess in general allocating the array yourself is the most efficient
though...

> I suppose you are talking about the toArray method defined in the
> java.util.Collection interface. Read the specification and you'll know
[quoted text clipped - 41 lines]
>>
>>PS: are there any classes or tools for speed measuring?
Eric Sosman - 14 Jun 2006 13:31 GMT
> Hi,
>
[quoted text clipped - 3 lines]
>     String[] names = (String[])party.toArray(new String[0]);
>     String[] names = (String[])party.toArray(new String[party.size()]);

    If `party' is non-empty, the first creates one more object
than the second.  This will probably take up a little more time
in object creation and may eventually make the garbage collector
work a tiny bit harder.

    On the other hand, the second makes one more method call
than the first, and we don't know how much time the size() method
of an arbitrary List implementation takes.

    Either way, any difference is likely to be so small as to
be very difficult to measure accurately.  Your question is a bit
like "Does waxing my car improve gas mileage by lowering wind
resistance, or hurt it by making the car larger and heavier?"

Signature

Eric Sosman
esosman@acm-dot-org.invalid

Oliver Wong - 14 Jun 2006 16:05 GMT
>     Either way, any difference is likely to be so small as to
> be very difficult to measure accurately.  Your question is a bit
> like "Does waxing my car improve gas mileage by lowering wind
> resistance, or hurt it by making the car larger and heavier?"

   Nice analogy. I'll try to remember and re-use it.

   - Oliver
Chris Uppal - 14 Jun 2006 16:34 GMT
> >     Either way, any difference is likely to be so small as to
> > be very difficult to measure accurately.  Your question is a bit
> > like "Does waxing my car improve gas mileage by lowering wind
> > resistance, or hurt it by making the car larger and heavier?"
>
>     Nice analogy. I'll try to remember and re-use it.

+1

   -- chris


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



©2009 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.