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 -> { ArrayListal = 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
środa, lutego 22, 2017
Czy ArrayList jest szybsze od LinkedList? #3
piątek, lutego 17, 2017
Czy ArrayList jest szybsze od LinkedList? #2
ArrayListal = new ArrayList<>(); for (int j=0; j < elements; j++) al.add(new Long(j)); LinkedList ll = new LinkedList<>(); for (int j=0; j < elements; j++) ll.add(new Long(j)); int indices[] = new int[elements / 100]; Random r = new Random(19700101); for (int i=0; i < indices.length; i++) { indices[i] = r.nextInt(indices.length); } t0 = System.currentTimeMillis(); BigDecimal sum = new BigDecimal(0); for (int i=0; i < indices.length; i++) { sum.add( new BigDecimal(al.get(indices[i])) ); } t1 = System.currentTimeMillis(); sum = new BigDecimal(0); for (int i=0; i < indices.length; i++) { sum.add( new BigDecimal(ll.get(indices[i])) ); } t2 = System.currentTimeMillis(); System.out.println("Access ArrayList: "+(t1-t0)+" ms, cnt="+elements+", tries="+indices.length); System.out.println("Access LinkedList: "+(t2-t1)+" ms, cnt="+elements+", tries="+indices.length); -- Access ArrayList: 12 ms, cnt=2000000, tries=20000 Access LinkedList: 371 ms, cnt=2000000, tries=20000
Czy ArrayList jest szybsze od LinkedList?
int elements = 1000000; int tries = 100; String values[] = new String[elements]; for (int k=0; k < elements; k++) { values[k] = String.valueOf(k).intern(); } long t0 = System.currentTimeMillis(); for (int i=0; i < tries; i++) { ArrayListal = new ArrayList (); for (int j=0; j < elements; j++) al.add(values[j]); } long t1= System.currentTimeMillis(); for (int i=0; i < tries; i++) { LinkedList ll = new LinkedList (); for (int j=0; j < elements; j++) ll.add(values[j]); } long t2 = System.currentTimeMillis(); System.out.println("Add to ArrayList: "+(t1-t0)/100+" ms avg, cnt="+elements+", tries="+tries); System.out.println("Add to LinkedList: "+(t2-t1)/100+" ms avg, cnt="+elements+", tries="+tries);
-- Add to ArrayList: 33 ms avg, cnt=2000000, tries=100 Add to LinkedList: 51 ms avg, cnt=2000000, tries=100 Add to ArrayList: 18 ms avg, cnt=1000000, tries=100 Add to LinkedList: 12 ms avg, cnt=1000000, tries=100
Subskrybuj:
Posty (Atom)