Created
January 30, 2014 15:52
-
-
Save chris-bernard/8711594 to your computer and use it in GitHub Desktop.
Array copy benchmark
This file contains hidden or 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
| package fr.amadeus.ws.pricexplorer; | |
| import com.google.common.base.Stopwatch; | |
| import java.util.Arrays; | |
| /** | |
| * Array copy benchmark | |
| * java -cp . -Xint ArrayCopierBenchmark performance 250000 | |
| */ | |
| public class ArrayCopierBenchmark { | |
| public static void main(String... arguments) { | |
| final int iterations = Integer.parseInt(arguments[1]); | |
| processArrayCopies(iterations); | |
| } | |
| /** | |
| * Display the time it takes to copy an array in various ways. | |
| */ | |
| private static void processArrayCopies(int iterations) { | |
| Stopwatch stopwatch = new Stopwatch(); | |
| int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; | |
| /* Copy by .clone() */ | |
| stopwatch.start(); | |
| copyUsingClone(numbers, iterations); | |
| stopwatch.stop(); | |
| log("Using clone: " + stopwatch); | |
| /* Copy by System.arraycopy() */ | |
| stopwatch.start(); | |
| copyUsingArraycopy(numbers, iterations); | |
| stopwatch.stop(); | |
| log("Using System.arraycopy: " + stopwatch); | |
| /* Copy by Arrays.copyOf() */ | |
| stopwatch.start(); | |
| copyUsingArraysCopyOf(numbers, iterations); | |
| stopwatch.stop(); | |
| log("Using Arrays.copyOf: " + stopwatch); | |
| /* Copy by loop */ | |
| stopwatch.start(); | |
| copyUsingForLoop(numbers, iterations); | |
| stopwatch.stop(); | |
| log("Using for loop: " + stopwatch); | |
| } | |
| /** | |
| * Copy by .clone() | |
| */ | |
| private static void copyUsingClone(int[] array, int iterations) { | |
| for (int idx = 0; idx < iterations; ++idx) { | |
| int[] copy = (int[]) array.clone(); | |
| } | |
| } | |
| /** | |
| * Copy by System.arraycopy() | |
| */ | |
| private static void copyUsingArraycopy(int[] array, int iterations) { | |
| for (int i = 0; i < iterations; ++i) { | |
| int[] copy = new int[array.length]; | |
| System.arraycopy(array, 0, copy, 0, array.length); | |
| } | |
| } | |
| /** | |
| * Copy by Arrays.copyOf() | |
| */ | |
| private static void copyUsingArraysCopyOf(int[] array, int iterations) { | |
| for (int i = 0; i < iterations; ++i) { | |
| int[] copy = Arrays.copyOf(array, array.length); | |
| } | |
| } | |
| /** | |
| * Copy by loop | |
| */ | |
| private static void copyUsingForLoop(int[] array, int iterations) { | |
| for (int i = 0; i < iterations; ++i) { | |
| int[] copy = new int[array.length]; | |
| for (int idx = 0; idx < array.length; ++idx) { | |
| copy[idx] = array[idx]; | |
| } | |
| } | |
| } | |
| private static void log(String message) { | |
| System.out.println(message); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment