Re: System.arrayCopy Vs array clone
- From: mei <mei@xxxxxx>
- Date: Wed, 28 Feb 2007 19:50:19 +0100
Hello Lew,
Lew a écrit :
No, it remains interesting. Notice that Bloch doesn't say "recommended" use.
I don't understand. What do you mean?
[...]
Since you actually measured the performance you have some facts about System.arrayCopy(). Would you be willing to share your benchmark code and how you ran it?
Hereunder is the benchmark code I wrote. Since I am not a specialist for writing such kind of code, if you detect any flaws, please let me know.
I used java hotspot 1.5.0_10
public class Bench {
private static final int[] tableau = {1, 2, 3, 4, 5, 6, 7, 8, 9};
private static final int count = 1000000;
static void testCopy() {
long startTime = System.currentTimeMillis();
long res = 0;
for (int i = 0; i < count; i++) {
int[] copy = new int[tableau.length];
System.arraycopy(tableau, 0, copy, 0, tableau.length);
for (int j = 0; j < copy.length; j++)
res += copy[j];
}
long endTime = System.currentTimeMillis();
System.out.println("testCopy(): dummy result=" + res + " time=" + (endTime - startTime));
}
static void testClone() {
long startTime = System.currentTimeMillis();
long res = 0;
for (int i = 0; i < count; i++) {
int[] copy = tableau.clone();
for (int j = 0; j < copy.length; j++)
res += copy[j];
}
long endTime = System.currentTimeMillis();
System.out.println("testClone(): dummy result=" + res + " time=" + (endTime - startTime));
}
public static void main(String[] args) {
testCopy();
testClone();
testCopy();
testClone();
testCopy();
testClone();
}
}
Here's a typical output:
testCopy(): dummy result=45000000 time=140
testClone(): dummy result=45000000 time=438
testCopy(): dummy result=45000000 time=125
testClone(): dummy result=45000000 time=437
testCopy(): dummy result=45000000 time=125
testClone(): dummy result=45000000 time=454
.
- References:
- System.arrayCopy Vs array clone
- From: mei
- Re: System.arrayCopy Vs array clone
- From: Daniel Pitts
- Re: System.arrayCopy Vs array clone
- From: Lew
- System.arrayCopy Vs array clone
- Prev by Date: Re: CVS graphic History visualization
- Next by Date: 3d solar system.
- Previous by thread: Re: System.arrayCopy Vs array clone
- Next by thread: Re: System.arrayCopy Vs array clone
- Index(es):