Jira BAEL-4211 (#11193)
* Init * Removing mvnw files * Apply eclipse code format * Refactoring * Refactoring * BAEL-4211 Add benchmarks * Delete hexagonal directory * Refactoring based on the feedback * Refactoring based on feedback - package rename * Directory rename Co-authored-by: asia <joannakrzeklubowiecka@protonmail.com>
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.copyarraymethodsperformance;
|
||||
|
||||
public class BenchmarkRunner {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
org.openjdk.jmh.Main.main(args);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.baeldung.copyarraymethodsperformance;
|
||||
|
||||
import org.openjdk.jmh.annotations.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@State(Scope.Thread)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
@Warmup(iterations = 10)
|
||||
@Fork(1)
|
||||
@Measurement(iterations = 100)
|
||||
public class ObjectsCopyBenchmark {
|
||||
|
||||
@Param({ "10", "1000000" })
|
||||
public int SIZE;
|
||||
Integer[] src;
|
||||
|
||||
@Setup
|
||||
public void setup() {
|
||||
Random r = new Random();
|
||||
src = new Integer[SIZE];
|
||||
|
||||
for (int i = 0; i < SIZE; i++) {
|
||||
src[i] = r.nextInt();
|
||||
}
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public Integer[] systemArrayCopyBenchmark() {
|
||||
Integer[] target = new Integer[SIZE];
|
||||
System.arraycopy(src, 0, target, 0, SIZE);
|
||||
return target;
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public Integer[] arraysCopyOfBenchmark() {
|
||||
return Arrays.copyOf(src, SIZE);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.baeldung.copyarraymethodsperformance;
|
||||
|
||||
import org.openjdk.jmh.annotations.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@State(Scope.Thread)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
@Warmup(iterations = 10)
|
||||
@Fork(1)
|
||||
@Measurement(iterations = 100)
|
||||
public class PrimitivesCopyBenchmark {
|
||||
|
||||
@Param({ "10", "1000000" })
|
||||
public int SIZE;
|
||||
|
||||
int[] src;
|
||||
|
||||
@Setup
|
||||
public void setup() {
|
||||
Random r = new Random();
|
||||
src = new int[SIZE];
|
||||
|
||||
for (int i = 0; i < SIZE; i++) {
|
||||
src[i] = r.nextInt();
|
||||
}
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public int[] systemArrayCopyBenchmark() {
|
||||
int[] target = new int[SIZE];
|
||||
System.arraycopy(src, 0, target, 0, SIZE);
|
||||
return target;
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public int[] arraysCopyOfBenchmark() {
|
||||
return Arrays.copyOf(src, SIZE);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user