Home | History | Annotate | Download | only in collect
      1 /*
      2  * Copyright (C) 2010 The Guava Authors
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  * http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.google.common.collect;
     18 
     19 import com.google.caliper.BeforeExperiment;
     20 import com.google.caliper.Benchmark;
     21 import com.google.caliper.Param;
     22 
     23 import java.util.ArrayList;
     24 import java.util.LinkedList;
     25 
     26 /**
     27  * Tests the speed of iteration of different iteration methods for collections.
     28  *
     29  * @author David Richter
     30  */
     31 public class IteratorBenchmark {
     32   @Param({"0", "1", "16", "256", "4096", "65536"}) int size;
     33 
     34   // use concrete classes to remove any possible polymorphic overhead?
     35   Object[] array;
     36   ArrayList<Object> arrayList;
     37   LinkedList<Object> linkedList;
     38 
     39   @BeforeExperiment void setUp() {
     40     array = new Object[size];
     41     arrayList = Lists.newArrayListWithCapacity(size);
     42     linkedList = Lists.newLinkedList();
     43 
     44     for (int i = 0; i < size; i++) {
     45       Object value = new Object();
     46       array[i] = value;
     47       arrayList.add(value);
     48       linkedList.add(value);
     49     }
     50   }
     51 
     52   @Benchmark int arrayIndexed(int reps) {
     53     int sum = 0;
     54     for (int i = 0; i < reps; i++) {
     55       for (int index = 0; index < size; index++) {
     56         sum += array[index].hashCode();
     57       }
     58     }
     59     return sum;
     60   }
     61 
     62   @Benchmark int arrayIndexedLength(int reps) {
     63     int sum = 0;
     64     for (int i = 0; i < reps; i++) {
     65       for (int index = 0; index < array.length; index++) {
     66         sum += array[index].hashCode();
     67       }
     68     }
     69     return sum;
     70   }
     71 
     72   @Benchmark int arrayFor(int reps) {
     73     int sum = 0;
     74     for (int i = 0; i < reps; i++) {
     75       for (Object value : array) {
     76         sum += value.hashCode();
     77       }
     78     }
     79     return sum;
     80   }
     81 
     82   @Benchmark int arrayListIndexed(int reps) {
     83     int sum = 0;
     84     for (int i = 0; i < reps; i++) {
     85       for (int index = 0; index < size; index++) {
     86         sum += arrayList.get(index).hashCode();
     87       }
     88     }
     89     return sum;
     90   }
     91 
     92   @Benchmark int arrayListIndexedLength(int reps) {
     93     int sum = 0;
     94     for (int i = 0; i < reps; i++) {
     95       for (int index = 0; index < arrayList.size(); index++) {
     96         sum += arrayList.get(index).hashCode();
     97       }
     98     }
     99     return sum;
    100   }
    101 
    102   @Benchmark int arrayListFor(int reps) {
    103     int sum = 0;
    104     for (int i = 0; i < reps; i++) {
    105       for (Object value : arrayList) {
    106         sum += value.hashCode();
    107       }
    108     }
    109     return sum;
    110   }
    111 
    112   @Benchmark int arrayListToArrayFor(int reps) {
    113     int sum = 0;
    114     for (int i = 0; i < reps; i++) {
    115       for (Object value : arrayList.toArray()) {
    116         sum += value.hashCode();
    117       }
    118     }
    119     return sum;
    120   }
    121 
    122   @Benchmark int linkedListFor(int reps) {
    123     int sum = 0;
    124     for (int i = 0; i < reps; i++) {
    125       for (Object value : linkedList) {
    126         sum += value.hashCode();
    127       }
    128     }
    129     return sum;
    130   }
    131 
    132   @Benchmark int linkedListToArrayFor(int reps) {
    133     int sum = 0;
    134     for (int i = 0; i < reps; i++) {
    135       for (Object value : linkedList.toArray()) {
    136         sum += value.hashCode();
    137       }
    138     }
    139     return sum;
    140   }
    141 }
    142