test/CollectionsSpeedTest.java
author František Kučera <franta-hg@frantovo.cz>
Thu Oct 13 03:09:22 2011 +0200 (2011-10-13)
changeset 75 41d6c0cac8b3
permissions -rwxr-xr-x
Drupal: číštění HTML – Tidy.
     1 /*
     2  *   StarOffice News Server
     3  *   see AUTHORS for the list of contributors
     4  *
     5  *   This program is free software: you can redistribute it and/or modify
     6  *   it under the terms of the GNU General Public License as published by
     7  *   the Free Software Foundation, either version 3 of the License, or
     8  *   (at your option) any later version.
     9  *
    10  *   This program is distributed in the hope that it will be useful,
    11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  *   GNU General Public License for more details.
    14  *
    15  *   You should have received a copy of the GNU General Public License
    16  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 
    19 package test;
    20 
    21 import java.util.ArrayList;
    22 import java.util.LinkedList;
    23 import java.util.List;
    24 import java.util.ListIterator;
    25 
    26 /**
    27  * Tests the speed of LinkedList and ArrayList.
    28  * @author Christian Lins
    29  * @since sonews/0.5.0
    30  */
    31 public class CollectionsSpeedTest 
    32 {
    33 
    34   public static void main(String[] args)
    35   {
    36     List arrayList  = new ArrayList();
    37     List linkedList = new LinkedList();
    38     
    39     int numElements = 100000;
    40     
    41     System.out.println("ArrayList.add(): " + add(arrayList, numElements) + "ms");
    42     System.out.println("LinkenList.add(): " + add(linkedList, numElements) + "ms");
    43     System.out.println("ArrayList.iterate: " + iterate(arrayList) + "ms");
    44     System.out.println("LinkedList.iterate: " + iterate(linkedList) + "ms");
    45   }
    46   
    47   private static long add(List list, int numElements)
    48   {
    49     long start = System.currentTimeMillis();
    50     
    51     for(int n = 0; n < numElements; n++)
    52     {
    53       list.add(new Object());
    54     }
    55     
    56     return System.currentTimeMillis() - start;
    57   }
    58   
    59   private static long iterate(List list)
    60   {
    61     long start = System.currentTimeMillis();
    62     
    63     ListIterator iter = list.listIterator();
    64     while(iter.hasNext())
    65     {
    66       iter.next();
    67     }
    68     
    69     return System.currentTimeMillis() - start;
    70   }
    71   
    72 }