Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.42 KB | None | 0 0
  1. package stack;
  2.  
  3. import java.util.HashMap;
  4. import java.util.LinkedList;
  5. import java.util.Random;
  6.  
  7. class Book1 {
  8.     // private - hermetyzacja
  9.     String author;
  10.     // private - hermetyzacja
  11.     String title;
  12.  
  13.     public Book1(String author, String title) {
  14.         this.author = author;
  15.         this.title = title;
  16.     }
  17.  
  18.     public String getAuthor() {
  19.  
  20.         return author;
  21.     }
  22.  
  23.     public String getTitle() {
  24.  
  25.         return title;
  26.     }
  27.  
  28.     public boolean equals(Object o) {
  29.         Book1 e = (Book1) o;
  30.         return author.equals(e.getAuthor()) && title.equals(e.getTitle());
  31.     }
  32.  
  33.     public int hashcode() {
  34.         return author.length();
  35.     }
  36.  
  37.     public String toString() {
  38.         return title + "-" + author;
  39.     }
  40. }
  41.  
  42. class SpeedTest {
  43.  
  44.     public static void main(String[] args) {
  45.  
  46.         LinkedList<Book1> milionsOfBooks = new LinkedList<>();
  47.  
  48.         HashMap<Book1, BookDetails> testMap = new HashMap<>();
  49.         Random random = new Random();
  50.  
  51.         // Pomysłowy sposób zapełnienia mapy :-)
  52.         for (int k = 0; k < 500000; k++) {
  53.             int numberOfA = random.nextInt(15) + 3;
  54.             int date = random.nextInt(17) + 2000;
  55.             double price = Math.random() * 1000 + 50;
  56.  
  57.             StringBuilder stringTitle = new StringBuilder();
  58.             StringBuilder stringAuthor = new StringBuilder();
  59.             for (int l = 0; l < numberOfA; l++) {
  60.                 stringTitle.append('a');
  61.                 stringAuthor.append('a');
  62.             }
  63.  
  64.  
  65.             Book1 book = new Book1(stringTitle.toString(), stringAuthor.toString());
  66.             BookDetails bookDetails = new BookDetails(date, price);
  67.             milionsOfBooks.add(book);
  68.             testMap.put(book, bookDetails);
  69.         }
  70.         System.out.println(milionsOfBooks);
  71.         System.out.println(testMap);
  72. // test objects to Add
  73.         Book1 testBook = new Book1("zzzzz", "zzzzzzz");
  74.         BookDetails testBookDetails = new BookDetails(2000, 50.5);
  75.  
  76. // LinkedList time measuring
  77.         long begin = System.currentTimeMillis();
  78.         // trzeba było przetestować metodę o sygnaturze: remove(Object o)
  79.         // a Ty przetestowałaś remove(int index) :) to też jest dobrze, ale w zadaniu chcieli tamtą konkretną z objectem,
  80.         // zmien wiec musisz zapisac sobie gdzies pierwsza dodana do kolekcji książkę, a potem wywołać i przetestowac czas:
  81.         // milionsOfBooks.remove(pierwszaDodanaKsiazka);
  82.         milionsOfBooks.remove(0);
  83.         long end = System.currentTimeMillis();
  84.         System.out.println("Removing first object from LinkedList has taken " + (end - begin) + " ms");
  85.  
  86.         begin = System.currentTimeMillis();
  87.         // to samo co z usuwaniem pierwszej, chcemy przetestowac remove(Object) a nie remove(int)
  88.         //musisz zapisac sobie gdzies ostatnia dodana do kolekcji książkę, a potem wywołać i przetestowac czas:
  89.         // milionsOfBooks.remove(ostatniaDodanaKsiazka);
  90.         milionsOfBooks.remove(milionsOfBooks.size() - 1);
  91.         end = System.currentTimeMillis();
  92.         System.out.println("Removing last object from LinkedList has taken " + (end - begin) + " ms");
  93.  
  94.         begin = System.currentTimeMillis();
  95.         // dobrze
  96.         milionsOfBooks.add(0, testBook);
  97.         end = System.currentTimeMillis();
  98.         System.out.println("Adding first object to LinkedList has taken " + (end - begin) + " ms");
  99.  
  100.         begin = System.currentTimeMillis();
  101.         // bardzo dobrze
  102.         milionsOfBooks.add(milionsOfBooks.size(), testBook);
  103.         end = System.currentTimeMillis();
  104.         System.out.println("Adding last object to LinkedList has taken " + (end - begin) + " ms");
  105.  
  106.  
  107. //HashMap time measuring
  108.  
  109.         begin = System.currentTimeMillis();
  110.         // to jest pobranie elementu o kluczu 500, a Ty nie masz takiego w kolekcji, to nie jest pobranie
  111.         // elementu nr 500
  112.         // powinnas zapisać sobie jakiś klucz który dodałaś do tej kolekcji (on jest typu Book1, bo zdefiniowalas:
  113.         // HashMap<Book1, BookDetails> testMap = new HashMap<>();
  114.         // czyli klucz typu Book1, wartosc dla klucza typu BookDetails
  115.         // i wtedy zrobic testMap.get(jakisObiektTypuBook1);
  116.         testMap.get(500);
  117.         end = System.currentTimeMillis();
  118.         System.out.println("Finding object by the key in the HashMap has taken " + (end - begin) + " ms");
  119.  
  120.         begin = System.currentTimeMillis();
  121.         testMap.put(testBook, testBookDetails);
  122.         end = System.currentTimeMillis();
  123.         System.out.println("Adding object to the HashMap has taken " + (end - begin) + " ms");
  124.  
  125.         begin = System.currentTimeMillis();
  126.        
  127.         // tutaj to samo co w testMap.get, powinno byc cos w stylu:
  128.         // testMap.remove(jakisObiektTypuBook1);
  129.         testMap.remove(8000000);
  130.         end = System.currentTimeMillis();
  131.         System.out.println("Removing object from HashMap has taken " + (end - begin) + " ms");
  132.  
  133.  
  134.     }
  135. }
  136.  
  137. class BookDetails {
  138.  
  139.     // private
  140.     int dateOfEdition;
  141.     // private
  142.     double price;
  143.  
  144.     public BookDetails(int dateOfEdition, double price) {
  145.         this.dateOfEdition = dateOfEdition;
  146.         this.price = price;
  147.     }
  148.  
  149.     public double getPrice() {
  150.         return price;
  151.     }
  152.  
  153.     public int getDateOfEdition() {
  154.         return dateOfEdition;
  155.     }
  156.  
  157.     public String toString() {
  158.         return dateOfEdition + "," + price;
  159.     }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement