-
-
Save plevart/974b67b65210f8dd122773f481c0a603 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.openjdk.jmh.annotations.Benchmark; | |
import org.openjdk.jmh.annotations.BenchmarkMode; | |
import org.openjdk.jmh.annotations.Fork; | |
import org.openjdk.jmh.annotations.Level; | |
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 java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.Collections; | |
import java.util.List; | |
import java.util.concurrent.TimeUnit; | |
@BenchmarkMode(Mode.AverageTime) | |
@Fork(value = 1) | |
@Warmup(iterations = 5, time = 1) | |
@Measurement(iterations = 10, time = 1) | |
@OutputTimeUnit(TimeUnit.NANOSECONDS) | |
@State(Scope.Benchmark) | |
public class ToListBench { | |
@Param({"10", "1000", "1000000"}) | |
public int len; | |
private Object[] array; | |
@Setup(Level.Trial) | |
public void setup() { | |
array = new Object[len]; | |
for (int i = 0; i < len; i++) { | |
array[i] = Boolean.TRUE; | |
} | |
} | |
@Benchmark | |
public List<Object> toList1() { | |
return Collections.unmodifiableList(new ArrayList<>(Arrays.asList(array))); | |
} | |
@Benchmark | |
public List<Object> toList2() { | |
return Arrays.asList(Arrays.copyOf(array, array.length, Object[].class)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment