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 import java.util.ArrayList;
23 import java.util.LinkedList;
24 
25 /**
26  * Tests the speed of iteration of different iteration methods for collections.
27  *
28  * @author David Richter
29  */
30 public class IteratorBenchmark {
31   @Param({"0", "1", "16", "256", "4096", "65536"})
32   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
setUp()40   void setUp() {
41     array = new Object[size];
42     arrayList = Lists.newArrayListWithCapacity(size);
43     linkedList = Lists.newLinkedList();
44 
45     for (int i = 0; i < size; i++) {
46       Object value = new Object();
47       array[i] = value;
48       arrayList.add(value);
49       linkedList.add(value);
50     }
51   }
52 
53   @Benchmark
arrayIndexed(int reps)54   int arrayIndexed(int reps) {
55     int sum = 0;
56     for (int i = 0; i < reps; i++) {
57       for (int index = 0; index < size; index++) {
58         sum += array[index].hashCode();
59       }
60     }
61     return sum;
62   }
63 
64   @Benchmark
arrayIndexedLength(int reps)65   int arrayIndexedLength(int reps) {
66     int sum = 0;
67     for (int i = 0; i < reps; i++) {
68       for (int index = 0; index < array.length; index++) {
69         sum += array[index].hashCode();
70       }
71     }
72     return sum;
73   }
74 
75   @Benchmark
arrayFor(int reps)76   int arrayFor(int reps) {
77     int sum = 0;
78     for (int i = 0; i < reps; i++) {
79       for (Object value : array) {
80         sum += value.hashCode();
81       }
82     }
83     return sum;
84   }
85 
86   @Benchmark
arrayListIndexed(int reps)87   int arrayListIndexed(int reps) {
88     int sum = 0;
89     for (int i = 0; i < reps; i++) {
90       for (int index = 0; index < size; index++) {
91         sum += arrayList.get(index).hashCode();
92       }
93     }
94     return sum;
95   }
96 
97   @Benchmark
arrayListIndexedLength(int reps)98   int arrayListIndexedLength(int reps) {
99     int sum = 0;
100     for (int i = 0; i < reps; i++) {
101       for (int index = 0; index < arrayList.size(); index++) {
102         sum += arrayList.get(index).hashCode();
103       }
104     }
105     return sum;
106   }
107 
108   @Benchmark
arrayListFor(int reps)109   int arrayListFor(int reps) {
110     int sum = 0;
111     for (int i = 0; i < reps; i++) {
112       for (Object value : arrayList) {
113         sum += value.hashCode();
114       }
115     }
116     return sum;
117   }
118 
119   @Benchmark
arrayListForWithHolder(int reps)120   int arrayListForWithHolder(int reps) {
121     int[] sumHolder = {0};
122     for (int i = 0; i < reps; i++) {
123       for (Object value : arrayList) {
124         sumHolder[0] += value.hashCode();
125       }
126     }
127     return sumHolder[0];
128   }
129 
130   @Benchmark
arrayListForEachWithHolder(int reps)131   int arrayListForEachWithHolder(int reps) {
132     int[] sumHolder = {0};
133     for (int i = 0; i < reps; i++) {
134       arrayList.forEach(value -> sumHolder[0] += value.hashCode());
135     }
136     return sumHolder[0];
137   }
138 
139   @Benchmark
arrayListToArrayFor(int reps)140   int arrayListToArrayFor(int reps) {
141     int sum = 0;
142     for (int i = 0; i < reps; i++) {
143       for (Object value : arrayList.toArray()) {
144         sum += value.hashCode();
145       }
146     }
147     return sum;
148   }
149 
150   @Benchmark
linkedListFor(int reps)151   int linkedListFor(int reps) {
152     int sum = 0;
153     for (int i = 0; i < reps; i++) {
154       for (Object value : linkedList) {
155         sum += value.hashCode();
156       }
157     }
158     return sum;
159   }
160 
161   @Benchmark
linkedListForEach(int reps)162   int linkedListForEach(int reps) {
163     int[] sumHolder = {0};
164     for (int i = 0; i < reps; i++) {
165       linkedList.forEach(value -> sumHolder[0] += value.hashCode());
166     }
167     return sumHolder[0];
168   }
169 
170   @Benchmark
linkedListToArrayFor(int reps)171   int linkedListToArrayFor(int reps) {
172     int sum = 0;
173     for (int i = 0; i < reps; i++) {
174       for (Object value : linkedList.toArray()) {
175         sum += value.hashCode();
176       }
177     }
178     return sum;
179   }
180 }
181