Guest User

Untitled

a guest
Feb 21st, 2018
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.27 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.         addToList(list, testAmount);
  12.         for(int counter = 0; counter < testTries; counter++) {
  13.             long beforeTime = System.nanoTime();
  14.             for (Integer e : list) {
  15.                 int integer = e.intValue();
  16.             }
  17.             long afterTime = System.nanoTime();
  18.             long timeSpent = afterTime - beforeTime;
  19.            
  20.             results.add(new Long(timeSpent));
  21.         }
  22.         long sum = 0;
  23.         for (Long e : results) {
  24.             sum += e;
  25.         }
  26.         return sum/results.size();
  27.     }
  28.    
  29.     public static void addToList(List<Integer> list, int amount) {
  30.         for(int counter = 0; counter < amount; counter++) {
  31.             list.add(new Integer(counter));
  32.         }
  33.     }
  34.    
  35.     public static void main(String[] args) {
  36.         ArrayList<Integer> arrayList = new ArrayList<Integer>();
  37.         LinkedList<Integer> linkedList = new LinkedList<Integer>();
  38.        
  39.         double arrayTest = testList(arrayList, 1000000, 10);
  40.         double linkedTest = testList(linkedList, 1000000, 10);
  41.         System.out.println("arrayTest avarage: "+arrayTest);
  42.         System.out.println("linkedTest avarage: "+linkedTest);
  43.     }
  44. }
Add Comment
Please, Sign In to add comment