gitweb on Svarog

projekti pod git sistemom za održavanje verzija -- projects under the git version control system
Poredjenje performansi razlicitih listi, doterivanja master
authorDoni Pracner <quinnuendo@gmail.com>
Wed, 6 May 2020 02:14:55 +0000 (04:14 +0200)
committerDoni Pracner <quinnuendo@gmail.com>
Wed, 6 May 2020 02:14:55 +0000 (04:14 +0200)
Reorganizacija koda na par mesta, popravljanje raznih ispisa da budu jasniji,
olaksavanje nekih doterivanja za buducnost.

kodovi/kolekcije/ListCompare.java

index 2059d059f7d791314635b2da460f3286365ead6f..19ff149e56a9bbfbb1ff9ce9aab51997474e835e 100644 (file)
@@ -26,25 +26,38 @@ import java.util.Random;
  */
 public class ListCompare {
 
-       private static final int SEQ_LENGTH = 500;
-       private static final int TEST_NUMBER = 100;
+       private static int seq_length = 1000;
+       private static int test_repeats = 2000;
 
        public static void main(String[] args) {
+               System.out.println(
+                               "Broj puta koliko će se testovi ponavljati:" + test_repeats);
+               System.out.println("* ponegde će biti naznačeno ako je drugačije");
+               System.out.println(
+                               "Sva vremena su izražena u nanosekundama, dato je prosečno vreme za jednu operaciju");
+               System.out.println();
+               System.out.println("al - ArrayList");
+               System.out.println("ll - LinkedList");
+               System.out.println("sl - naša sortirana lista");
+               System.out.println();
                // generate random sequence of numbers
-               int[] seq = genRandomSequence(SEQ_LENGTH, new Random(123456789));
+               int[] seq = genRandomSequence(seq_length, new Random(123456789));
+               int[] seqs = genRandomSequence(seq_length / 10, new Random(123456789));
                // insert into ArrayList and LinkedList
                System.out.println("--- ubacivanje ---");
                compareNormalInsertion();
 
                System.out.println("--- contains operacija ---");
-               compareContains(1000);
+               compareContains(seq);
 
                System.out.println("--- sortirano ubacivanje ---");
-               compareSortedInsertion(seq);
-
-               System.out.printf("custom insertion%n  :%1$10.2f%n", customSortedInsertionTestMulti(seq, TEST_NUMBER));
-               System.out.printf("custom insertion%n  :%1$10.2f%n", customSortedInsertionTestMulti(seq, TEST_NUMBER));
+               System.out.println("broj elemenata:" + (seq_length / 10));
+               compareSortedInsertion(seqs);
 
+               System.out.printf("sl:%1$12.2f%n",
+                               customSortedInsertionTestMulti(seqs, test_repeats));
+               System.out.printf("sl:%1$12.2f%n",
+                               customSortedInsertionTestMulti(seqs, test_repeats));
                System.out.println("--- uklanjanja ---");
                compareRemoval(10000);
        }
@@ -57,18 +70,20 @@ public class ListCompare {
        }
 
        private static void compareNormalInsertion() {
-               int[] seq = genRandomSequence(10 * SEQ_LENGTH, new Random(123456789));
+               int duz = 10 * seq_length;
+               int[] seq = genRandomSequence(duz, new Random(123456789));
                ArrayList<Integer> al = new ArrayList<>();
                LinkedList<Integer> ll = new LinkedList<>();
-               int nt = TEST_NUMBER;
-               System.out.println("Normal insertion test");
-               System.out.printf("ll:%1$10.2f%n", insertionTestMulti(ll, seq, nt));
-               System.out.printf("al:%1$10.2f%n", insertionTestMulti(al, seq, nt));
-               System.out.printf("al:%1$10.2f%n", insertionTestMulti(al, seq, nt));
-               System.out.printf("ll:%1$10.2f%n", insertionTestMulti(ll, seq, nt));
+               int nt = test_repeats / 20;
+               System.out.printf("Broj brojeva: %1d; broj testiranja: %2d%n", duz, nt);
+               System.out.printf("ll:%1$12.2f%n", insertionTestMulti(ll, seq, nt));
+               System.out.printf("al:%1$12.2f%n", insertionTestMulti(al, seq, nt));
+               System.out.printf("al:%1$12.2f%n", insertionTestMulti(al, seq, nt));
+               System.out.printf("ll:%1$12.2f%n", insertionTestMulti(ll, seq, nt));
        }
 
-       private static double insertionTestMulti(List<Integer> l, int[] seq, int testNumber) {
+       private static double insertionTestMulti(List<Integer> l, int[] seq,
+                       int testNumber) {
                long sum = 0;
                for (int i = 0; i < testNumber; i++) {
                        try {
@@ -93,14 +108,19 @@ public class ListCompare {
                ArrayList<Integer> al = new ArrayList<>();
                LinkedList<Integer> ll = new LinkedList<>();
 
-               System.out.println("Sorted insertion test");
-               System.out.printf("ll:%1$10.2f%n", sortedInsertionTestMulti(ll, seq, TEST_NUMBER));
-               System.out.printf("al:%1$10.2f%n", sortedInsertionTestMulti(al, seq, TEST_NUMBER));
-               System.out.printf("al:%1$10.2f%n", sortedInsertionTestMulti(al, seq, TEST_NUMBER));
-               System.out.printf("ll:%1$10.2f%n", sortedInsertionTestMulti(ll, seq, TEST_NUMBER));
+               System.out.println("Sortirano ubacivanje");
+               System.out.printf("ll:%1$12.2f%n",
+                               sortedInsertionTestMulti(ll, seq, test_repeats));
+               System.out.printf("al:%1$12.2f%n",
+                               sortedInsertionTestMulti(al, seq, test_repeats));
+               System.out.printf("al:%1$12.2f%n",
+                               sortedInsertionTestMulti(al, seq, test_repeats));
+               System.out.printf("ll:%1$12.2f%n",
+                               sortedInsertionTestMulti(ll, seq, test_repeats));
        }
 
-       private static double sortedInsertionTestMulti(List<Integer> l, int[] seq, int numtests) {
+       private static double sortedInsertionTestMulti(List<Integer> l, int[] seq,
+                       int numtests) {
                long sum = 0;
                for (int i = 0; i < numtests; i++) {
                        try {
@@ -123,13 +143,14 @@ public class ListCompare {
 
        private static void insertSorted(int num, List<Integer> l) {
                int i = 0;
-               while (i < l.size() && l.get(i) < num) {
+               while (i < l.size() && l.get(i).compareTo(num) < 0) {
                        i++;
                }
                l.add(i, num);
        }
 
-       private static double customSortedInsertionTestMulti(int[] seq, int numtests) {
+       private static double customSortedInsertionTestMulti(int[] seq,
+                       int numtests) {
                long sum = 0;
                for (int i = 0; i < numtests; i++) {
                        sum += customSortedInsertion(seq);
@@ -156,38 +177,55 @@ public class ListCompare {
                        ll.add(i);
                        l.dodaj(i);
                }
-               System.out.println("test izbacivanja; duzina:" + seqLength);
-               System.out.println("izbacivanje nepostojeceg:");
+               System.out.println("test izbacivanja; dužina:" + seqLength);
+               System.out.println("izbacivanje nepostojećeg:");
                double perc = -1.0;
-               int testNumber = TEST_NUMBER;
-               System.out.printf("al:%1$10.2f\n", removalTestMulti(al, seqLength + 2, testNumber, perc));
-               System.out.printf("ll:%1$10.2f\n", removalTestMulti(ll, seqLength + 2, testNumber, perc));
-               System.out.printf("ll:%1$10.2f\n", removalTestMulti(ll, seqLength + 2, testNumber, perc));
-               System.out.printf("al:%1$10.2f\n", removalTestMulti(al, seqLength + 2, testNumber, perc));
+               int testNumber = test_repeats;
+               System.out.printf("al:%1$12.2f\n",
+                               removalTestMulti(al, seqLength + 2, testNumber, perc));
+               System.out.printf("ll:%1$12.2f\n",
+                               removalTestMulti(ll, seqLength + 2, testNumber, perc));
+               System.out.printf("ll:%1$12.2f\n",
+                               removalTestMulti(ll, seqLength + 2, testNumber, perc));
+               System.out.printf("al:%1$12.2f\n",
+                               removalTestMulti(al, seqLength + 2, testNumber, perc));
 
                perc = 0.1;
-               System.out.println("izbacivanje postojeceg na procentu:" + perc);
-               System.out.printf("al:%1$10.2f\n", removalTestMulti(al, seqLength + 2, testNumber, perc));
-               System.out.printf("ll:%1$10.2f\n", removalTestMulti(ll, seqLength + 2, testNumber, perc));
-               System.out.printf("ll:%1$10.2f\n", removalTestMulti(ll, seqLength + 2, testNumber, perc));
-               System.out.printf("al:%1$10.2f\n", removalTestMulti(al, seqLength + 2, testNumber, perc));
+               System.out.println("izbacivanje postojećeg na procentu:" + perc);
+               System.out.printf("al:%1$12.2f\n",
+                               removalTestMulti(al, seqLength + 2, testNumber, perc));
+               System.out.printf("ll:%1$12.2f\n",
+                               removalTestMulti(ll, seqLength + 2, testNumber, perc));
+               System.out.printf("ll:%1$12.2f\n",
+                               removalTestMulti(ll, seqLength + 2, testNumber, perc));
+               System.out.printf("al:%1$12.2f\n",
+                               removalTestMulti(al, seqLength + 2, testNumber, perc));
 
                perc = 0.5;
-               System.out.println("izbacivanje postojeceg na procentu:" + perc);
-               System.out.printf("al:%1$10.2f\n", removalTestMulti(al, seqLength + 2, testNumber, perc));
-               System.out.printf("ll:%1$10.2f\n", removalTestMulti(ll, seqLength + 2, testNumber, perc));
-               System.out.printf("ll:%1$10.2f\n", removalTestMulti(ll, seqLength + 2, testNumber, perc));
-               System.out.printf("al:%1$10.2f\n", removalTestMulti(al, seqLength + 2, testNumber, perc));
+               System.out.println("izbacivanje postojećeg na procentu:" + perc);
+               System.out.printf("al:%1$12.2f\n",
+                               removalTestMulti(al, seqLength + 2, testNumber, perc));
+               System.out.printf("ll:%1$12.2f\n",
+                               removalTestMulti(ll, seqLength + 2, testNumber, perc));
+               System.out.printf("ll:%1$12.2f\n",
+                               removalTestMulti(ll, seqLength + 2, testNumber, perc));
+               System.out.printf("al:%1$12.2f\n",
+                               removalTestMulti(al, seqLength + 2, testNumber, perc));
 
                perc = 0.8;
-               System.out.println("izbacivanje postojeceg na procentu:" + perc);
-               System.out.printf("al:%1$10.2f\n", removalTestMulti(al, seqLength + 2, testNumber, perc));
-               System.out.printf("ll:%1$10.2f\n", removalTestMulti(ll, seqLength + 2, testNumber, perc));
-               System.out.printf("ll:%1$10.2f\n", removalTestMulti(ll, seqLength + 2, testNumber, perc));
-               System.out.printf("al:%1$10.2f\n", removalTestMulti(al, seqLength + 2, testNumber, perc));
+               System.out.println("izbacivanje postojećeg na procentu:" + perc);
+               System.out.printf("al:%1$12.2f\n",
+                               removalTestMulti(al, seqLength + 2, testNumber, perc));
+               System.out.printf("ll:%1$12.2f\n",
+                               removalTestMulti(ll, seqLength + 2, testNumber, perc));
+               System.out.printf("ll:%1$12.2f\n",
+                               removalTestMulti(ll, seqLength + 2, testNumber, perc));
+               System.out.printf("al:%1$12.2f\n",
+                               removalTestMulti(al, seqLength + 2, testNumber, perc));
        }
 
-       private static double removalTestMulti(List<Integer> l, int num, int testNumber, double percentWhere) {
+       private static double removalTestMulti(List<Integer> l, int num,
+                       int testNumber, double percentWhere) {
                long sum = 0;
                for (int i = 0; i < testNumber; i++) {
                        if (percentWhere >= 0) {
@@ -200,9 +238,9 @@ public class ListCompare {
                return ((double) sum) / testNumber;
        }
 
-       private static void compareContains(int seqLength) {
-
-               int[] seq = genRandomSequence(seqLength, new Random(123456789));
+       private static void compareContains(int[] seq) {
+               int seqLength = seq.length;
+               System.out.println("Broj brojeva:" + seqLength);
                ArrayList<Integer> al = new ArrayList<>();
                LinkedList<Integer> ll = new LinkedList<>();
                SortiranaListaBrojeva l = new SortiranaListaBrojeva();
@@ -212,33 +250,48 @@ public class ListCompare {
                        l.dodaj(i);
                }
 
-               System.out.println("nepostojeci:");
-               System.out.printf("contains al:%1$10.2f%n" , containsTestMulti(al, seqLength + 1, TEST_NUMBER));
-               System.out.printf("contains ll:%1$10.2f%n" , containsTestMulti(ll, seqLength + 1, TEST_NUMBER));
-               System.out.printf("for-rucn al:%1$10.2f%n" , forSearchTestMulti(al, seqLength + 1, TEST_NUMBER));
-               System.out.printf("for-rucn ll:%1$10.2f%n" , forSearchTestMulti(ll, seqLength + 1, TEST_NUMBER));
-               System.out.printf("for-each al:%1$10.2f%n" , forEachSearchTestMulti(al, seqLength + 1, TEST_NUMBER));
-               System.out.printf("for-each ll:%1$10.2f%n" , forEachSearchTestMulti(ll, seqLength + 1, TEST_NUMBER));
-
-               System.out.printf("custom sort:%1$10.2f%n" , customContainsSortedTestMulti(l, seqLength + 1, TEST_NUMBER));
+               System.out.println("- nepostojeći (-1):");
+               System.out.printf("contains al:%1$12.2f%n",
+                               containsTestMulti(al, -1, test_repeats));
+               System.out.printf("contains ll:%1$12.2f%n",
+                               containsTestMulti(ll, -1, test_repeats));
+               System.out.printf("petlja   al:%1$12.2f%n",
+                               loopSearchTestMulti(al, -1, test_repeats));
+               System.out.printf("petlja   ll:%1$12.2f%n",
+                               loopSearchTestMulti(ll, -1, test_repeats));
+               System.out.printf("for-each al:%1$12.2f%n",
+                               forEachSearchTestMulti(al, -1, test_repeats));
+               System.out.printf("for-each ll:%1$12.2f%n",
+                               forEachSearchTestMulti(ll, -1, test_repeats));
+
+               System.out.printf("sl uListi  :%1$12.2f%n",
+                               customContainsSortedTestMulti(l, -1, test_repeats));
 
                al.set(seqLength / 3, seqLength + 1);
                ll.set(seqLength / 3, seqLength + 1);
                // ovaj moramo uzeti jer je sortirano
                int el3 = l.element(seqLength / 3);
-               System.out.println("postojeci:");
-
-               System.out.printf("contains al:%1$10.2f%n" , containsTestMulti(al, seqLength + 1, TEST_NUMBER));
-               System.out.printf("contains ll:%1$10.2f%n" , containsTestMulti(ll, seqLength + 1, TEST_NUMBER));
-               System.out.printf("for-rucn al:%1$10.2f%n" , forSearchTestMulti(al, seqLength + 1, TEST_NUMBER));
-               System.out.printf("for-rucn ll:%1$10.2f%n" , forSearchTestMulti(ll, seqLength + 1, TEST_NUMBER));
-               System.out.printf("for-each al:%1$10.2f%n" , forEachSearchTestMulti(al, seqLength + 1, TEST_NUMBER));
-               System.out.printf("for-each ll:%1$10.2f%n" , forEachSearchTestMulti(ll, seqLength + 1, TEST_NUMBER));
-
-               System.out.printf("custom sort:%1$10.2f%n" , customContainsSortedTestMulti(l, el3, TEST_NUMBER));
+               System.out.println("- postojeći (na trećini dužine):");
+
+               System.out.printf("contains al:%1$12.2f%n",
+                               containsTestMulti(al, seqLength + 1, test_repeats));
+               System.out.printf("contains ll:%1$12.2f%n",
+                               containsTestMulti(ll, seqLength + 1, test_repeats));
+               System.out.printf("for-ručn al:%1$12.2f%n",
+                               loopSearchTestMulti(al, seqLength + 1, test_repeats));
+               System.out.printf("for-ručn ll:%1$12.2f%n",
+                               loopSearchTestMulti(ll, seqLength + 1, test_repeats));
+               System.out.printf("for-each al:%1$12.2f%n",
+                               forEachSearchTestMulti(al, seqLength + 1, test_repeats));
+               System.out.printf("for-each ll:%1$12.2f%n",
+                               forEachSearchTestMulti(ll, seqLength + 1, test_repeats));
+
+               System.out.printf("sl uListi  :%1$12.2f%n",
+                               customContainsSortedTestMulti(l, el3, test_repeats));
        }
 
-       private static double containsTestMulti(List<Integer> l, int num, int numTests) {
+       private static double containsTestMulti(List<Integer> l, int num,
+                       int numTests) {
                long res = 0;
                for (int i = 0; i < numTests; i++) {
                        long curr = System.nanoTime();
@@ -248,13 +301,15 @@ public class ListCompare {
                return ((double) res) / numTests;
        }
 
-       private static double forSearchTestMulti(List<Integer> l, int num, int numTests) {
+       private static double loopSearchTestMulti(List<Integer> l, int num,
+                       int numTests) {
                long res = 0;
                for (int i = 0; i < numTests; i++) {
                        long curr = System.nanoTime();
 
                        int j = 0;
-                       while (j < l.size() && !l.get(j).equals(num))
+                       int size = l.size();
+                       while (j < size && !l.get(j).equals(num))
                                j++;
 
                        res += System.nanoTime() - curr;
@@ -262,13 +317,14 @@ public class ListCompare {
                return ((double) res) / numTests;
        }
 
-       private static double forEachSearchTestMulti(List<Integer> l, int num, int numTests) {
+       private static double forEachSearchTestMulti(List<Integer> l, int num,
+                       int numTests) {
                long res = 0;
                for (int i = 0; i < numTests; i++) {
                        long curr = System.nanoTime();
 
-                       for (int el : l)
-                               if (el == num)
+                       for (Integer el : l)
+                               if (el.equals(num))
                                        break;
 
                        res += System.nanoTime() - curr;
@@ -276,7 +332,8 @@ public class ListCompare {
                return ((double) res) / numTests;
        }
 
-       private static double customContainsSortedTestMulti(SortiranaListaBrojeva l, int num, int numTests) {
+       private static double customContainsSortedTestMulti(SortiranaListaBrojeva l,
+                       int num, int numTests) {
                long res = 0;
                for (int i = 0; i < numTests; i++) {
                        long curr = System.nanoTime();
@@ -335,7 +392,8 @@ class SortiranaListaBrojeva {
                        if (!prvi.info.equals(s)) {
                                Element prethodni = prvi;
 
-                               while (prethodni.veza != null && prethodni.veza.info.compareTo(s) < 0) {
+                               while (prethodni.veza != null
+                                               && prethodni.veza.info.compareTo(s) < 0) {
                                        prethodni = prethodni.veza;
                                }
                                Element novi = new Element(s);
@@ -362,8 +420,7 @@ class SortiranaListaBrojeva {
                if (tekuci != null) {
                        return tekuci.info;
                } else {
-                       throw new IndexOutOfBoundsException("Lista je kraca od "
-                                       + k);
+                       throw new IndexOutOfBoundsException("Lista je kraca od " + k);
                }
        }
 
Svarog.pmf.uns.ac.rs/gitweb maintanance Doni Pracner