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