Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package plotnikov;
- import dnl.utils.text.table.TextTable;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.LinkedList;
- public class EfficiencyTester {
- int roundAmount;
- int startAmount;
- double insertValue;
- double scale;
- private ArrayList<Double> arrayList = new ArrayList<>();
- private LinkedList<Double> linkedList = new LinkedList<>();
- public EfficiencyTester(int roundAmount, int startAmount, double insertValue, double scale) {
- this.roundAmount = roundAmount;
- this.startAmount = startAmount;
- this.insertValue = insertValue;
- this.scale = scale;
- }
- private void resultsToString(String msgBefore, long[][] results) {
- int currentAmount = startAmount;
- String[][] tableData = new String[roundAmount][4];
- for (int i = 0; i < roundAmount; i++) {
- tableData[i][0] = String.valueOf(currentAmount);
- tableData[i][1] = String.valueOf(results[0][i]);
- tableData[i][2] = String.valueOf(results[1][i]);
- tableData[i][3] = String.valueOf(results[0][i] - results[1][i]);
- currentAmount *= scale;
- }
- String[] columnNames = {" Amount (n) ", " ArrayList (ms) ", " LinkedList (ms) ", " Difference (ms) "};
- TextTable table = new TextTable(columnNames, tableData);
- System.out.println(msgBefore);
- table.printTable();
- System.out.println("\n");
- }
- public void testAdd() {
- long[][] results = new long[2][roundAmount];
- int currentAmount = startAmount;
- for (int i = 0; i < roundAmount; i++) {
- long startTime, endTime;
- startTime = System.nanoTime();
- for (int j = 0; j < currentAmount; j++) {
- arrayList.add(insertValue);
- }
- endTime = System.nanoTime();
- arrayList.clear();
- results[0][i] = endTime - startTime;
- startTime = System.nanoTime();
- for (int j = 0; j < currentAmount; j++) {
- linkedList.add(insertValue);
- }
- endTime = System.nanoTime();
- linkedList.clear();
- results[1][i] = endTime - startTime;
- currentAmount *= scale;
- }
- resultsToString(" Add test:", results);
- }
- public void testGet() {
- long[][] results = new long[2][roundAmount];
- int currentAmount = startAmount;
- for (int i = 0; i < roundAmount; i++) {
- long startTime, endTime;
- arrayList = new ArrayList<Double>(Collections.nCopies(currentAmount, insertValue));
- startTime = System.nanoTime();
- for (int j = 0; j < currentAmount; j++) {
- arrayList.get(j);
- }
- endTime = System.nanoTime();
- results[0][i] = endTime - startTime;
- arrayList.clear();
- linkedList = new LinkedList<Double>(Collections.nCopies(currentAmount, insertValue));
- startTime = System.nanoTime();
- for (int j = 0; j < currentAmount; j++) {
- linkedList.get(j);
- }
- endTime = System.nanoTime();
- results[1][i] = endTime - startTime;
- linkedList.clear();
- currentAmount *= scale;
- }
- resultsToString(" Get test:", results);
- }
- public void testRemoveFromEnd() {
- long[][] results = new long[2][roundAmount];
- int currentAmount = startAmount;
- for (int i = 0; i < roundAmount; i++) {
- long startTime, endTime;
- arrayList = new ArrayList<Double>(Collections.nCopies(currentAmount, insertValue));
- startTime = System.nanoTime();
- for (int j = currentAmount - 1; j >= 0; j--) {
- arrayList.remove(j);
- }
- endTime = System.nanoTime();
- results[0][i] = endTime - startTime;
- arrayList.clear();
- linkedList = new LinkedList<Double>(Collections.nCopies(currentAmount, insertValue));
- startTime = System.nanoTime();
- for (int j = currentAmount - 1; j >= 0; j--) {
- linkedList.remove(j);
- }
- endTime = System.nanoTime();
- results[1][i] = endTime - startTime;
- linkedList.clear();
- currentAmount *= scale;
- }
- resultsToString(" Remove from end test:", results);
- }
- public void testRemoveFromBegin() {
- long[][] results = new long[2][roundAmount];
- int currentAmount = startAmount;
- for (int i = 0; i < roundAmount; i++) {
- long startTime, endTime;
- arrayList = new ArrayList<Double>(Collections.nCopies(currentAmount, insertValue));
- startTime = System.nanoTime();
- for (int j = 0; j < currentAmount; j++) {
- arrayList.remove(0);
- }
- endTime = System.nanoTime();
- results[0][i] = endTime - startTime;
- arrayList.clear();
- linkedList = new LinkedList<Double>(Collections.nCopies(currentAmount, insertValue));
- startTime = System.nanoTime();
- for (int j = 0; j < currentAmount; j++) {
- linkedList.remove(0);
- }
- endTime = System.nanoTime();
- results[1][i] = endTime - startTime;
- linkedList.clear();
- currentAmount *= scale;
- }
- resultsToString(" Remove from begin test:", results);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement