package se.juneday.trials; import java.util.ArrayList; import java.util.Collections; import java.util.LinkedList; import java.util.List; import java.util.Iterator; import se.juneday.domain.Contact; import se.juneday.util.Kreator; public class TryList { private enum ListType { ARRAY_LIST, LINKED_LIST } private enum Action { SORT, ADD, REMOVE, FIND, NAIVE_FIND } public ListType listType = ListType.ARRAY_LIST; private int nrContacts = 10_000_000; private List contacts; private Action action = Action.SORT; private void sortTest() { long start = System.currentTimeMillis(); Collections.sort(contacts); long stop = System.currentTimeMillis(); System.out.println("sort | " + contacts.size() + " contacts | " + contacts.getClass().getSimpleName() + " | " + (stop - start) + " milli seconds"); } private void removeTest(int position) { long start = System.currentTimeMillis(); contacts.remove(position); long stop = System.currentTimeMillis(); System.out.println("remove | " + "at " + position + " | "+ contacts.getClass().getSimpleName() + " | " + (stop - start) + " milli seconds"); } private void addTest(Contact contact, int position) { int size = contacts.size(); long start = System.currentTimeMillis(); contacts.add(contact); long stop = System.currentTimeMillis(); System.out.println("add | " + "at " + position + " | "+ contacts.getClass().getSimpleName() + " | " + (stop - start) + " milli seconds"); } private void findTest() { // System.out.println("Inserting contact at position: " + position + ". Now: " + contacts.size() + " elements in the list"); long sortStart = System.currentTimeMillis(); Collections.sort(contacts); long sortStop = System.currentTimeMillis(); //System.out.println("Sorting the list took: " + (stop-start) + " milli seconds"); // Get the Contact in the middle int replacePosition = contacts.size()/2; Contact old = contacts.get(replacePosition); // Replace Contact with a new Contact with a slightly altered name Contact unique = new Contact(old.name()+" _unique", old.email(), old.phone()); contacts.set(replacePosition, unique); // Sort again Collections.sort(contacts); long start = System.currentTimeMillis(); int pos = Collections.binarySearch(contacts, unique); long stop = System.currentTimeMillis(); if (pos<0) { System.out.println("Failed finding : " + unique.name()); }// else { // System.out.println("Found \"" + contacts.get(pos).name() + "\" at position: " + pos); // } System.out.println("find | " + "at " + pos + " | "+ contacts.getClass().getSimpleName() + " | " + (stop - start) + " milli seconds | " + (sortStop - sortStart) + " milli seconds sorting "); } private void findNaiveTest() { // Get the Contact in the middle int replacePosition = contacts.size()/2; Contact old = contacts.get(replacePosition); // Replace Contact with a new Contact with a slightly altered name Contact unique = new Contact(old.name()+"_unique", old.email(), old.phone()); contacts.set(replacePosition, unique); int pos = -1; long start = System.currentTimeMillis(); int i=0; Iterator it = contacts.iterator(); while (it.hasNext()) { i++; Contact c = (Contact) it.next(); if (c.equals(unique)) { pos = i; break; } } long stop = System.currentTimeMillis(); if (pos<0) { System.out.println("Failed finding : " + unique.name()); }// else { // System.out.println("Found \"" + contacts.get(pos).name() + "\" at position: " + pos); // } System.out.println("naive find | " + "at " + pos + " | "+ contacts.getClass().getSimpleName() + " | " + (stop - start) + " milli seconds"); } private static void usage() { System.out.println("TryList [OPTIONS] "); System.out.println(); System.out.println("OPTIONS"); System.out.println(" --linked-list, -ll use LinkedList"); System.out.println(" --array-list, -al use ArrayList (default)"); // System.out.println(" --contacts nr number of elements. Default: " + nrContacts); System.out.println(); System.out.println("TARGETS"); System.out.println(" sort sorting list (default)"); System.out.println(" remove remove elements"); System.out.println(" add add elements"); System.out.println(" find find elements"); System.out.println(" naive find elements naively"); System.out.println(); } public void parseArgs(String[] args) { for(int i=0; i(nrContacts); } else { contacts = new LinkedList<>(); } // System.out.println(" * Created: " + contacts.getClass().getSimpleName()); for (int i=0; i contacts = new ArrayList<>(nrContacts); /* */ /* */ } }