Advertisement
Sanlover

Untitled

Nov 23rd, 2021
678
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.53 KB | None | 0 0
  1. package plotnikov;
  2.  
  3. import dnl.utils.text.table.TextTable;
  4.  
  5. import java.util.ArrayList;
  6. import java.util.Collections;
  7. import java.util.LinkedList;
  8.  
  9. public class EfficiencyTester {
  10.     int roundAmount;
  11.     int startAmount;
  12.     double insertValue;
  13.     double scale;
  14.     private ArrayList<Double> arrayList = new ArrayList<>();
  15.     private LinkedList<Double> linkedList = new LinkedList<>();
  16.  
  17.     public EfficiencyTester(int roundAmount, int startAmount, double insertValue, double scale) {
  18.         this.roundAmount = roundAmount;
  19.         this.startAmount = startAmount;
  20.         this.insertValue = insertValue;
  21.         this.scale = scale;
  22.     }
  23.  
  24.     private void resultsToString(String msgBefore, long[][] results) {
  25.         int currentAmount = startAmount;
  26.  
  27.         String[][] tableData = new String[roundAmount][4];
  28.         for (int i = 0; i < roundAmount; i++) {
  29.             tableData[i][0] = String.valueOf(currentAmount);
  30.             tableData[i][1] = String.valueOf(results[0][i]);
  31.             tableData[i][2] = String.valueOf(results[1][i]);
  32.             tableData[i][3] = String.valueOf(results[0][i] - results[1][i]);
  33.             currentAmount *= scale;
  34.         }
  35.         String[] columnNames = {" Amount (n) ", " ArrayList (ms) ", " LinkedList (ms) ", " Difference (ms) "};
  36.         TextTable table = new TextTable(columnNames, tableData);
  37.         System.out.println(msgBefore);
  38.         table.printTable();
  39.         System.out.println("\n");
  40.     }
  41.  
  42.     public void testAdd() {
  43.         long[][] results = new long[2][roundAmount];
  44.  
  45.         int currentAmount = startAmount;
  46.         for (int i = 0; i < roundAmount; i++) {
  47.             long startTime, endTime;
  48.             startTime = System.nanoTime();
  49.             for (int j = 0; j < currentAmount; j++) {
  50.                 arrayList.add(insertValue);
  51.             }
  52.             endTime = System.nanoTime();
  53.             arrayList.clear();
  54.             results[0][i] = endTime - startTime;
  55.  
  56.             startTime = System.nanoTime();
  57.             for (int j = 0; j < currentAmount; j++) {
  58.                 linkedList.add(insertValue);
  59.             }
  60.             endTime = System.nanoTime();
  61.             linkedList.clear();
  62.             results[1][i] = endTime - startTime;
  63.  
  64.             currentAmount *= scale;
  65.         }
  66.         resultsToString("   Add test:", results);
  67.     }
  68.  
  69.     public void testGet() {
  70.         long[][] results = new long[2][roundAmount];
  71.  
  72.         int currentAmount = startAmount;
  73.         for (int i = 0; i < roundAmount; i++) {
  74.             long startTime, endTime;
  75.             arrayList = new ArrayList<Double>(Collections.nCopies(currentAmount, insertValue));
  76.             startTime = System.nanoTime();
  77.             for (int j = 0; j < currentAmount; j++) {
  78.                 arrayList.get(j);
  79.             }
  80.             endTime = System.nanoTime();
  81.             results[0][i] = endTime - startTime;
  82.             arrayList.clear();
  83.             linkedList = new LinkedList<Double>(Collections.nCopies(currentAmount, insertValue));
  84.             startTime = System.nanoTime();
  85.             for (int j = 0; j < currentAmount; j++) {
  86.                 linkedList.get(j);
  87.             }
  88.             endTime = System.nanoTime();
  89.             results[1][i] = endTime - startTime;
  90.             linkedList.clear();
  91.  
  92.             currentAmount *= scale;
  93.         }
  94.         resultsToString("   Get test:", results);
  95.     }
  96.  
  97.     public void testRemoveFromEnd() {
  98.         long[][] results = new long[2][roundAmount];
  99.  
  100.         int currentAmount = startAmount;
  101.         for (int i = 0; i < roundAmount; i++) {
  102.             long startTime, endTime;
  103.             arrayList = new ArrayList<Double>(Collections.nCopies(currentAmount, insertValue));
  104.             startTime = System.nanoTime();
  105.             for (int j = currentAmount - 1; j >= 0; j--) {
  106.                 arrayList.remove(j);
  107.             }
  108.             endTime = System.nanoTime();
  109.             results[0][i] = endTime - startTime;
  110.             arrayList.clear();
  111.             linkedList = new LinkedList<Double>(Collections.nCopies(currentAmount, insertValue));
  112.             startTime = System.nanoTime();
  113.             for (int j = currentAmount - 1; j >= 0; j--) {
  114.                 linkedList.remove(j);
  115.             }
  116.             endTime = System.nanoTime();
  117.             results[1][i] = endTime - startTime;
  118.             linkedList.clear();
  119.  
  120.             currentAmount *= scale;
  121.         }
  122.         resultsToString("   Remove from end test:", results);
  123.     }
  124.  
  125.     public void testRemoveFromBegin() {
  126.         long[][] results = new long[2][roundAmount];
  127.  
  128.         int currentAmount = startAmount;
  129.         for (int i = 0; i < roundAmount; i++) {
  130.             long startTime, endTime;
  131.             arrayList = new ArrayList<Double>(Collections.nCopies(currentAmount, insertValue));
  132.             startTime = System.nanoTime();
  133.             for (int j = 0; j < currentAmount; j++) {
  134.                 arrayList.remove(0);
  135.             }
  136.             endTime = System.nanoTime();
  137.             results[0][i] = endTime - startTime;
  138.             arrayList.clear();
  139.             linkedList = new LinkedList<Double>(Collections.nCopies(currentAmount, insertValue));
  140.             startTime = System.nanoTime();
  141.             for (int j = 0; j < currentAmount; j++) {
  142.                 linkedList.remove(0);
  143.             }
  144.             endTime = System.nanoTime();
  145.             results[1][i] = endTime - startTime;
  146.             linkedList.clear();
  147.  
  148.             currentAmount *= scale;
  149.         }
  150.         resultsToString("   Remove from begin test:", results);
  151.     }
  152. }
  153.  
  154.  
  155.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement