blob: a8cfb05a7cd72eca54fa7a6f18624b80dc614e07 [file] [log] [blame]
Jesse Wilson1440b362009-12-15 18:54:02 -08001/*
2 * Copyright (C) 2009 Google Inc.
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
Jesse Wilsonf062bf42010-01-13 17:12:18 -080017package examples;
Jesse Wilson1440b362009-12-15 18:54:02 -080018
19import com.google.caliper.Param;
20import com.google.caliper.Runner;
21import com.google.caliper.SimpleBenchmark;
Jesse Wilson1440b362009-12-15 18:54:02 -080022import java.util.AbstractList;
Jesse Wilson1440b362009-12-15 18:54:02 -080023import java.util.List;
24
25/**
26 * Measures iterating through list elements.
27 */
28public class ListIterationBenchmark extends SimpleBenchmark {
Jesse Wilson1440b362009-12-15 18:54:02 -080029
Jesse Wilsonf062bf42010-01-13 17:12:18 -080030 @Param({"0", "10", "100", "1000"})
31 private int length;
Jesse Wilson1440b362009-12-15 18:54:02 -080032
33 private List<Object> list;
34 private Object[] array;
35
36 @Override protected void setUp() {
37 array = new Object[length];
38 for (int i = 0; i < length; i++) {
39 array[i] = new Object();
40 }
41
42 list = new AbstractList<Object>() {
43 @Override public int size() {
44 return length;
45 }
46
47 @Override public Object get(int i) {
48 return array[i];
49 }
50 };
51 }
52
Jesse Wilsonf062bf42010-01-13 17:12:18 -080053 @SuppressWarnings({"UnusedDeclaration"}) // TODO: fix
54 public void timeListIteration(int reps) {
Jesse Wilson1440b362009-12-15 18:54:02 -080055 for (int i = 0; i < reps; i++) {
56 for (Object value : list) {
Jesse Wilson1440b362009-12-15 18:54:02 -080057 }
58 }
Jesse Wilson1440b362009-12-15 18:54:02 -080059 }
60
Jesse Wilsonf062bf42010-01-13 17:12:18 -080061 @SuppressWarnings({"UnusedDeclaration"}) // TODO: fix
62 public void timeArrayIteration(int reps) {
Jesse Wilson1440b362009-12-15 18:54:02 -080063 for (int i = 0; i < reps; i++) {
64 for (Object value : array) {
Jesse Wilson1440b362009-12-15 18:54:02 -080065 }
66 }
Jesse Wilson1440b362009-12-15 18:54:02 -080067 }
68
69 // TODO: remove this from all examples when IDE plugins are ready
70 public static void main(String[] args) throws Exception {
71 Runner.main(ListIterationBenchmark.class, args);
72 }
73}