Jesse Wilson | 1440b36 | 2009-12-15 18:54:02 -0800 | [diff] [blame] | 1 | /* |
| 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 Wilson | f062bf4 | 2010-01-13 17:12:18 -0800 | [diff] [blame^] | 17 | package examples; |
Jesse Wilson | 1440b36 | 2009-12-15 18:54:02 -0800 | [diff] [blame] | 18 | |
| 19 | import com.google.caliper.Param; |
| 20 | import com.google.caliper.Runner; |
| 21 | import com.google.caliper.SimpleBenchmark; |
Jesse Wilson | 1440b36 | 2009-12-15 18:54:02 -0800 | [diff] [blame] | 22 | import java.util.AbstractList; |
Jesse Wilson | 1440b36 | 2009-12-15 18:54:02 -0800 | [diff] [blame] | 23 | import java.util.List; |
| 24 | |
| 25 | /** |
| 26 | * Measures iterating through list elements. |
| 27 | */ |
| 28 | public class ListIterationBenchmark extends SimpleBenchmark { |
Jesse Wilson | 1440b36 | 2009-12-15 18:54:02 -0800 | [diff] [blame] | 29 | |
Jesse Wilson | f062bf4 | 2010-01-13 17:12:18 -0800 | [diff] [blame^] | 30 | @Param({"0", "10", "100", "1000"}) |
| 31 | private int length; |
Jesse Wilson | 1440b36 | 2009-12-15 18:54:02 -0800 | [diff] [blame] | 32 | |
| 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 Wilson | f062bf4 | 2010-01-13 17:12:18 -0800 | [diff] [blame^] | 53 | @SuppressWarnings({"UnusedDeclaration"}) // TODO: fix |
| 54 | public void timeListIteration(int reps) { |
Jesse Wilson | 1440b36 | 2009-12-15 18:54:02 -0800 | [diff] [blame] | 55 | for (int i = 0; i < reps; i++) { |
| 56 | for (Object value : list) { |
Jesse Wilson | 1440b36 | 2009-12-15 18:54:02 -0800 | [diff] [blame] | 57 | } |
| 58 | } |
Jesse Wilson | 1440b36 | 2009-12-15 18:54:02 -0800 | [diff] [blame] | 59 | } |
| 60 | |
Jesse Wilson | f062bf4 | 2010-01-13 17:12:18 -0800 | [diff] [blame^] | 61 | @SuppressWarnings({"UnusedDeclaration"}) // TODO: fix |
| 62 | public void timeArrayIteration(int reps) { |
Jesse Wilson | 1440b36 | 2009-12-15 18:54:02 -0800 | [diff] [blame] | 63 | for (int i = 0; i < reps; i++) { |
| 64 | for (Object value : array) { |
Jesse Wilson | 1440b36 | 2009-12-15 18:54:02 -0800 | [diff] [blame] | 65 | } |
| 66 | } |
Jesse Wilson | 1440b36 | 2009-12-15 18:54:02 -0800 | [diff] [blame] | 67 | } |
| 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 | } |