package VL13.Aufgabe03; import java.util.ArrayList; import java.util.LinkedList; import java.util.Random; /** * Vorlesung 13 / Aufgabe 3 * * @author Sebastian Brosch */ public class Aufgabe03 { public static void main(String[] args) { final int MAX_RUNS = 10; int[] sizes = { 10000, 20000, 50000, 100000, 200000, 500000, 1000000, 2000000, 5000000, 10000000, 20000000, 50000000 }; for (int size : sizes) { long sumArrayList = 0; long sumLinkedList = 0; for (int i = 0; i < MAX_RUNS; i++) { ArrayList values = getValues(size); sumArrayList += runTestArrayList(values); sumLinkedList += runTestLinkedList(values); } System.out.printf("\nMittelwert (Zeichenketten: %,d):\n", size); System.out.printf(" ArrayList: %d Millisekunden\n", (sumArrayList / MAX_RUNS)); System.out.printf(" LinkedList: %d Millisekunden\n", (sumLinkedList / MAX_RUNS)); } System.out.println("\nAlle Tests wurden beendet."); } /** * Method to run a test using a ArrayList. * * @param values The values to fill in ArrayList. * @return The elapsed time of the test. */ private static long runTestArrayList(ArrayList values) { long timeStart = System.currentTimeMillis(); ArrayList testArrayList = new ArrayList(); for (String value : values) { testArrayList.add(value); } return System.currentTimeMillis() - timeStart; } /** * Method to run a test using LinkedList. * * @param values The values to fill in LinkedList. * @return The elapsed time of the test. */ private static long runTestLinkedList(ArrayList values) { long timeStart = System.currentTimeMillis(); LinkedList testLinkedList = new LinkedList(); for (String value : values) { testLinkedList.add(value); } return System.currentTimeMillis() - timeStart; } /** * Method to determine a random number from a certain range. * * @param start The first number of the range. * @param end The last number of the range. * @return A random number from a certain range. */ private static int getRandomNumber(int start, int end) { return (new Random()).nextInt((end - start + 1)) + start; } /** * Method to generate a random string. * * @param min The minimum length of the random string. * @param max The maximum length of the random string. * @return A random string. */ private static String getRandomString(int min, int max) { final String NUMBERS = "0123456789"; final String UCHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; final String LCHARS = UCHARS.toLowerCase(); String random = ""; int length = getRandomNumber(min, max); for (int i = 0; i < length; i++) { String chars = ""; switch (getRandomNumber(1, 3)) { case 1: chars = NUMBERS; break; case 2: chars = UCHARS; break; case 3: chars = LCHARS; break; } int charIndex = getRandomNumber(0, chars.length() - 1); random += chars.substring(charIndex, charIndex + 1); } return random; } /** * Method to get a list with random strings. * * @param numberOfStrings The amount of random strings to generate. * @return A list with random strings. */ private static ArrayList getValues(int numberOfStrings) { final int MIN_LENGTH = 10; final int MAX_LENGTH = 16; ArrayList values = new ArrayList(); for (int i = 0; i < numberOfStrings; i++) { values.add(getRandomString(MIN_LENGTH, MAX_LENGTH)); } return values; } }