Skip to content

Instantly share code, notes, and snippets.

@schlosna
Last active February 8, 2023 04:04
Show Gist options
  • Save schlosna/975e26965ec822ad42034b3ea2b08676 to your computer and use it in GitHub Desktop.
Save schlosna/975e26965ec822ad42034b3ea2b08676 to your computer and use it in GitHub Desktop.
System.arraycopy vs. array.clone()
package org.openjdk.micro;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.OptionsBuilder;
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Warmup(iterations = 2, time = 3, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 15, time = 1, timeUnit = TimeUnit.SECONDS)
@Fork(1)
@State(Scope.Benchmark)
public class ArrayCopyBenchmark {
@Param({"0", "10", "100", "1000"})
int size;
private byte[] bytes;
private int[] ints;
@Setup
public void setup() {
bytes = new byte[size];
ints = new int[size];
ThreadLocalRandom.current().nextBytes(bytes);
for (int i = 0; i < ints.length; i++) {
ints[i] = ThreadLocalRandom.current().nextInt();
}
}
@Benchmark
public byte[] byteClone() {
return bytes.clone();
}
@Benchmark
public byte[] byteArraycopy() {
byte[] copy = new byte[bytes.length];
System.arraycopy(bytes, 0, copy, 0, bytes.length);
return copy;
}
@Benchmark
public int[] intClone() {
return ints.clone();
}
@Benchmark
public int[] intArraycopy() {
int[] copy = new int[ints.length];
System.arraycopy(ints, 0, copy, 0, ints.length);
return copy;
}
public static void main(String[] _args) throws Exception {
new Runner(new OptionsBuilder()
.include(ArrayCopyBenchmark.class.getSimpleName())
.build())
.run();
}
}
VM version: JDK 17.0.6, OpenJDK 64-Bit Server VM, 17.0.6+10-LTS
M1 Pro arm64
Benchmark (size) Mode Cnt Score Error Units
ArrayCopyBenchmark.byteArraycopy 10 avgt 15 2.913 ± 0.017 ns/op
ArrayCopyBenchmark.byteArraycopy 100 avgt 15 4.636 ± 0.145 ns/op
ArrayCopyBenchmark.byteArraycopy 1000 avgt 15 26.047 ± 0.320 ns/op
ArrayCopyBenchmark.byteClone 10 avgt 15 2.919 ± 0.021 ns/op
ArrayCopyBenchmark.byteClone 100 avgt 15 4.808 ± 0.585 ns/op
ArrayCopyBenchmark.byteClone 1000 avgt 15 27.421 ± 3.639 ns/op
ArrayCopyBenchmark.intArraycopy 10 avgt 15 3.621 ± 0.043 ns/op
ArrayCopyBenchmark.intArraycopy 100 avgt 15 11.854 ± 0.113 ns/op
ArrayCopyBenchmark.intArraycopy 1000 avgt 15 119.846 ± 5.181 ns/op
ArrayCopyBenchmark.intClone 10 avgt 15 3.643 ± 0.112 ns/op
ArrayCopyBenchmark.intClone 100 avgt 15 12.814 ± 0.041 ns/op
ArrayCopyBenchmark.intClone 1000 avgt 15 128.883 ± 22.079 ns/op
VM version: JDK 17.0.6, OpenJDK 64-Bit Server VM, 17.0.6+10-LTS
Intel(R) Xeon(R) Platinum 8259CL CPU @ 2.50GHz
Benchmark (size) Mode Cnt Score Error Units
ArrayCopyBenchmark.byteArraycopy 0 avgt 5 3.057 ± 0.065 ns/op
ArrayCopyBenchmark.byteArraycopy 100 avgt 5 12.539 ± 0.278 ns/op
ArrayCopyBenchmark.byteArraycopy 1000 avgt 5 129.562 ± 4.205 ns/op
ArrayCopyBenchmark.byteArraycopy 100000 avgt 5 15401.425 ± 217.063 ns/op
ArrayCopyBenchmark.byteClone 0 avgt 5 4.476 ± 0.081 ns/op
ArrayCopyBenchmark.byteClone 100 avgt 5 12.318 ± 0.086 ns/op
ArrayCopyBenchmark.byteClone 1000 avgt 5 129.285 ± 2.803 ns/op
ArrayCopyBenchmark.byteClone 100000 avgt 5 15525.481 ± 49.255 ns/op
ArrayCopyBenchmark.intArraycopy 0 avgt 5 3.092 ± 0.030 ns/op
ArrayCopyBenchmark.intArraycopy 100 avgt 5 43.432 ± 0.521 ns/op
ArrayCopyBenchmark.intArraycopy 1000 avgt 5 497.230 ± 7.689 ns/op
ArrayCopyBenchmark.intArraycopy 100000 avgt 5 61643.946 ± 757.288 ns/op
ArrayCopyBenchmark.intClone 0 avgt 5 4.630 ± 0.091 ns/op
ArrayCopyBenchmark.intClone 100 avgt 5 43.942 ± 0.323 ns/op
ArrayCopyBenchmark.intClone 1000 avgt 5 497.280 ± 11.368 ns/op
ArrayCopyBenchmark.intClone 100000 avgt 5 61974.111 ± 276.052 ns/op
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment