środa, lutego 22, 2017

Czy ArrayList jest szybsze od LinkedList? #3

int elements = 2000000;
int tries = 100;
String values[] = new String[elements];
IntStream.range(0, elements).forEach( el -> { values[el]=el+""; } );
  
long t0 = System.currentTimeMillis();
IntStream.range(0, tries).parallel().forEach( i -> {
 ArrayList al = new ArrayList();
 IntStream.range(0, elements).parallel().forEach( j -> { al.add(values[j]); } );
});  
  
long t1= System.currentTimeMillis();
IntStream.range(0, tries).parallel().forEach( i -> {
 LinkedList ll = new LinkedList();
 IntStream.range(0, elements).parallel().forEach( j -> { ll.add(values[j]); } );
});   
  
  
long t2 = System.currentTimeMillis();
System.out.println("Add to ArrayList: "+(t1-t0)/tries+" ms avg, cnt="+elements+", tries="+tries);
System.out.println("Add to LinkedList: "+(t2-t1)/tries+" ms avg, cnt="+elements+", tries="+tries);

--
Add to ArrayList: 18 ms avg, cnt=2000000, tries=100
Add to LinkedList: 77 ms avg, cnt=2000000, tries=100

0 komentarze: