Uebungsaufgaben/Semester2/Vorlesung2part3.java
2024-05-14 01:22:50 +02:00

49 lines
1.6 KiB
Java

package Semester2;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Random;
public class Vorlesung2part3 {
public static void main(String[] args) {
LinkedList<String> linkedList = new LinkedList<>();
ArrayList<String> arrayList = new ArrayList<>();
int[] anzahl = {10000, 20000, 50000, 100000, 200000, 500000,
1000000, 2000000, 5000000, 10000000, 20000000, 50000000};
for (int i: anzahl) {
long timeStartLinked = System.currentTimeMillis();
for (int j = 0; j < i; j++) {
linkedList.add(generiereZeichenkette());
}
long timeEndLinked = System.currentTimeMillis();
long elapsedTimeLinked = timeEndLinked - timeStartLinked;
System.out.println("Linked List time for " + i + " Data: " + elapsedTimeLinked);
long timeStartArray = System.currentTimeMillis();
for (int j = 0; j < i; j++) {
arrayList.add(generiereZeichenkette());
}
long timeEndArray = System.currentTimeMillis();
long elapsedTimeArray = timeEndArray - timeStartArray;
System.out.println("Array List time for " + i + " Data: " + elapsedTimeArray);
System.out.println("\n");
linkedList.clear();
arrayList.clear();
}
}
private static String generiereZeichenkette() {
Random random = new Random();
String wort = "";
for (int i = 0; i < random.nextInt(15, 20); i++) {
wort += (char) random.nextInt(65, 122);
}
return wort;
}
}