Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #30692 > unrolled thread

Recommended way to make shallow copy of array

Started byv_borchert@despammed.com (Volker Borchert)
First post2015-01-13 19:50 +0000
Last post2015-01-19 20:15 +0000
Articles 6 — 4 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  Recommended way to make shallow copy of array v_borchert@despammed.com (Volker Borchert) - 2015-01-13 19:50 +0000
    Re: Recommended way to make shallow copy of array Joerg Meier <joergmmeier@arcor.de> - 2015-01-13 22:40 +0100
      Re: Recommended way to make shallow copy of array Joerg Meier <joergmmeier@arcor.de> - 2015-01-13 22:44 +0100
    Re: Recommended way to make shallow copy of array Sebastian <news@seyweiler.dyndns.org> - 2015-01-13 23:04 +0100
      Re: Recommended way to make shallow copy of array Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-01-13 18:00 -0500
      Re: Recommended way to make shallow copy of array v_borchert@despammed.com (Volker Borchert) - 2015-01-19 20:15 +0000

#30692 — Recommended way to make shallow copy of array

Fromv_borchert@despammed.com (Volker Borchert)
Date2015-01-13 19:50 +0000
SubjectRecommended way to make shallow copy of array
Message-ID<m93su6$n1u$1@Gaia.teknon.de>
Given an array of runtime component type T, I want to create a new
array of the same runtime component type referencing the same objects.
The two ways I know to achieve this are Arrays.copyOf() and T[].clone().
Target VM is Hotspot Client of Sun JDK 1.7.0_72, will move on to Sun
JDK 1.8.0_<then-current> some time not so far in the future. Which
method should I prefer? Any other suggestions? Primary use case are
defensive copies, where T is some immutable type or a primitive.

TIA


-- 

"I'm a doctor, not a mechanic." Dr Leonard McCoy <mccoy@ncc1701.starfleet.fed>
"I'm a mechanic, not a doctor." Volker Borchert  <v_borchert@despammed.com>

[toc] | [next] | [standalone]


#30693

FromJoerg Meier <joergmmeier@arcor.de>
Date2015-01-13 22:40 +0100
Message-ID<idqbq8nuuijb.18az9qzevjyns$.dlg@40tude.net>
In reply to#30692
On 13 Jan 2015 19:50:30 GMT, Volker Borchert wrote:

> Given an array of runtime component type T, I want to create a new
> array of the same runtime component type referencing the same objects.
> The two ways I know to achieve this are Arrays.copyOf() and T[].clone().
> Target VM is Hotspot Client of Sun JDK 1.7.0_72, will move on to Sun
> JDK 1.8.0_<then-current> some time not so far in the future. Which
> method should I prefer? Any other suggestions? Primary use case are
> defensive copies, where T is some immutable type or a primitive.

Since .clone uses Reflection, it is extremely slow (relatively speaking). I
seem to recall someone benchmarking both methods (as well as a good old
loop) and found .clone to be slower by at least two orders of magnitude.

Liebe Gruesse,
		Joerg

-- 
Ich lese meine Emails nicht, replies to Email bleiben also leider
ungelesen.

[toc] | [prev] | [next] | [standalone]


#30694

FromJoerg Meier <joergmmeier@arcor.de>
Date2015-01-13 22:44 +0100
Message-ID<1swyt2hzlndzx$.1mnj3gfg1vapq.dlg@40tude.net>
In reply to#30693
On Tue, 13 Jan 2015 22:40:07 +0100, Joerg Meier wrote:

> Since .clone uses Reflection, it is extremely slow (relatively speaking). I
> seem to recall someone benchmarking both methods (as well as a good old
> loop) and found .clone to be slower by at least two orders of magnitude.

Please disregard above advice. It seems my answer was at best horribly
outdated. A cursory glance at Google results indicates that speed of .clone
and Arrays.copyOf (and System.arraycopy) are about the same. My bad.

Liebe Gruesse,
		Joerg

-- 
Ich lese meine Emails nicht, replies to Email bleiben also leider
ungelesen.

[toc] | [prev] | [next] | [standalone]


#30695

FromSebastian <news@seyweiler.dyndns.org>
Date2015-01-13 23:04 +0100
Message-ID<m944pq$58h$1@news.albasani.net>
In reply to#30692
Am 13.01.2015 20:50, schrieb Volker Borchert:
> Given an array of runtime component type T, I want to create a new
> array of the same runtime component type referencing the same objects.
> The two ways I know to achieve this are Arrays.copyOf() and T[].clone().
> Target VM is Hotspot Client of Sun JDK 1.7.0_72, will move on to Sun
> JDK 1.8.0_<then-current> some time not so far in the future. Which
> method should I prefer? Any other suggestions? Primary use case are
> defensive copies, where T is some immutable type or a primitive.
>
> TIA
>
>

There are several ways to copy an array :

- use the various copyOf and copyOfRange methods of the Arrays class

- use System.arraycopy (useful in particular when copying parts of an 
array)

- call its clone method, and do a cast (the simplest style, suitable for 
a shallow clone)

- use a for-loop (verbose)

The for-loop, copyOf and System.arraycopy styles clearly have 
independent storage, a clone of a one-dimensional array has independent 
storage, but the clone of a multi-dimensional array does not.

System.arraycopy seems to have slightly better performance. 
Arrays.copyOf is easiest to use with dynamic typing.

Differences between the various methods are small, however, and might be 
regarded a micro-optimization.

-- Sebastian

[toc] | [prev] | [next] | [standalone]


#30699

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2015-01-13 18:00 -0500
Message-ID<m94813$2nd$1@dont-email.me>
In reply to#30695
On 1/13/2015 5:04 PM, Sebastian wrote:
> Am 13.01.2015 20:50, schrieb Volker Borchert:
>> Given an array of runtime component type T, I want to create a new
>> array of the same runtime component type referencing the same objects.
>> The two ways I know to achieve this are Arrays.copyOf() and T[].clone().
>> Target VM is Hotspot Client of Sun JDK 1.7.0_72, will move on to Sun
>> JDK 1.8.0_<then-current> some time not so far in the future. Which
>> method should I prefer? Any other suggestions? Primary use case are
>> defensive copies, where T is some immutable type or a primitive.
>>
>> TIA
>>
>>
>
> There are several ways to copy an array :
>[...]
> - call its clone method, and do a cast (the simplest style, suitable for
> a shallow clone)

     Why cast?

     "The return type of the clone method of an array type T[] is T[]."

http://docs.oracle.com/javase/specs/jls/se8/html/jls-10.html#jls-10.7

-- 
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM

[toc] | [prev] | [next] | [standalone]


#30750

Fromv_borchert@despammed.com (Volker Borchert)
Date2015-01-19 20:15 +0000
Message-ID<m9jokq$3ih$1@Gaia.teknon.de>
In reply to#30695
Sebastian wrote:

> - use System.arraycopy (useful in particular when copying parts of an 
> array)

That's what Array.copyOf() and Arrays.copyOfRange() do.

> The for-loop, copyOf and System.arraycopy styles clearly have 
> independent storage, a clone of a one-dimensional array has independent 
> storage, but the clone of a multi-dimensional array does not.
> 
> System.arraycopy seems to have slightly better performance. 
> Arrays.copyOf is easiest to use with dynamic typing.

The question is, what happens when the JIT kicks in and starts
inlining and/or intrinsifying...

> Differences between the various methods are small, however, and might be 
> regarded a micro-optimization.

OK that should do, at least for now and until proven wrong.

Thanks


-- 

"I'm a doctor, not a mechanic." Dr Leonard McCoy <mccoy@ncc1701.starfleet.fed>
"I'm a mechanic, not a doctor." Volker Borchert  <v_borchert@despammed.com>

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web