Add 3 3 (ArrayList vs LinkedList)

This commit is contained in:
Matti 2024-05-09 16:54:44 +02:00
parent 97ae6d91b6
commit f5319538ad
2 changed files with 63 additions and 7 deletions

View File

@ -10,6 +10,7 @@
<br>Sowohl SourceCode, PDF als auch Word Datei
- VL 2 Aufgabe 1 (Erstellen von Testdaten)
- VL 2 Aufgabe 2 (StringTokenizer)
- VL 3 Aufgabe 3 (ArrayList vs LinkedList)
- keine

View File

@ -1,5 +1,8 @@
package part3.aufg3;
import java.util.ArrayList;
import java.util.LinkedList;
/**
* Aufgabe 3
* Es soll die Einfügeperformance der beiden Klassen LinkedList und ArrayList verglichen
@ -23,24 +26,76 @@ package part3.aufg3;
public class Anwendung {
private String getRandomString(){
public static String getRandomString(int MIN_LENGTH, int MAX_LENGTH) {
char[] alphabet = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
int size;
do {
size = (int) Math.random()*16;
}while (size<10);
size = (int) (Math.random() * MAX_LENGTH);
} while (size < MIN_LENGTH);
String output = "";
int index;
for (int i = 0; i < size; i++) {
index = (int) Math.random()*alphabet.length;
index = (int) (Math.random() * alphabet.length);
output += alphabet[index];
}
return output;
}
public static void fillWithRandomStrings(LinkedList<String> ls, int amount) {
for (int i = 0; i < amount; i++) {
ls.add(getRandomString(10, 16));
}
}
public static void fillWithRandomStrings(ArrayList<String> ls, int amount){
for (int i=0; i<amount; i++){
ls.add(getRandomString(10,16));
}
}
public static void main(String[] args) {
int stringsToAdd = 50_000_000;
long totalTimeSpent = 0;
int measurements = 10;
for (int i = 0; i < measurements; i++) {
long timeStart = System.currentTimeMillis();
// LinkedList<String> ls = new LinkedList<>();
ArrayList<String> ls = new ArrayList<>();
fillWithRandomStrings(ls, stringsToAdd);
long timeEnd = System.currentTimeMillis();
long timeSpent = timeEnd-timeStart;
totalTimeSpent += timeSpent;
System.out.println(timeSpent);
}
System.out.println("Average of " + measurements + ": " + (totalTimeSpent/measurements));
}
}
/*
Linked List: ArrayLis:
10k 10 single Time of 0 ??? 7
20k 9 Here too ?? 11
50k 24 22
100k 47 37
200k 99 74
500k 245 188
1Mil 737 350
2Mil 1576 688
5Mil 4173 1650
10Mil 8600 3300
20Mil 17750 6690
50Mil 43600 24860
*/