Guest User

Untitled

a guest
Feb 21st, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.22 KB | None | 0 0
  1. package com.javadaemon.skrollr.test;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.LinkedList;
  5. import java.util.List;
  6.  
  7. public class ListSpeedTest {
  8.    
  9.     public static double testList(List<Integer> list, int testAmount, int testTries) {
  10.         ArrayList<Long> results = new ArrayList<Long>();
  11.         for(int counter = 0; counter < testTries; counter++) {
  12.             long beforeTime = System.nanoTime();
  13.             addToList(list, testAmount);
  14.             long afterTime = System.nanoTime();
  15.             long timeSpent = afterTime - beforeTime;
  16.             list.clear();
  17.             results.add(new Long(timeSpent));
  18.         }
  19.         long sum = 0;
  20.         for (Long e : results) {
  21.             sum += e;
  22.         }
  23.         return sum/results.size();
  24.     }
  25.    
  26.     public static void addToList(List<Integer> list, int amount) {
  27.         for(int counter = 0; counter < amount; counter++) {
  28.             list.add(new Integer(counter));
  29.         }
  30.     }
  31.    
  32.     public static void main(String[] args) {
  33.         ArrayList<Integer> arrayList = new ArrayList<Integer>();
  34.         LinkedList<Integer> linkedList = new LinkedList<Integer>();
  35.        
  36.         double arrayTest = testList(arrayList, 1000000, 10);
  37.         double linkedTest = testList(linkedList, 1000000, 10);
  38.         System.out.println("arrayTest avarage: "+arrayTest);
  39.         System.out.println("linkedTest avarage: "+linkedTest);
  40.     }
  41. }
Add Comment
Please, Sign In to add comment