Jesse Wilson | e95457f | 2009-12-10 15:45:23 -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 | |
| 17 | package com.google.caliper.examples; |
| 18 | |
| 19 | import com.google.caliper.Benchmark; |
| 20 | import com.google.caliper.DefaultBenchmarkSuite; |
| 21 | import com.google.caliper.Param; |
| 22 | import com.google.caliper.Runner; |
| 23 | |
| 24 | import java.util.Collection; |
| 25 | import java.util.EnumSet; |
| 26 | import java.util.Set; |
| 27 | |
| 28 | /** |
| 29 | * Measures EnumSet#contains(). |
| 30 | */ |
| 31 | public class EnumSetContainsBenchmarkSuite extends DefaultBenchmarkSuite { |
| 32 | |
| 33 | @Param private SetMaker setMaker; |
| 34 | |
| 35 | private static Collection<SetMaker> setMakerValues = EnumSet.allOf(SetMaker.class); |
| 36 | |
| 37 | enum SetMaker { |
| 38 | ENUM_SET { |
| 39 | @Override Set<?> newSet() { |
| 40 | return EnumSet.allOf(RegularSize.class); |
| 41 | } |
| 42 | @Override Object[] testValues() { |
| 43 | return new Object[] { RegularSize.E1, RegularSize.E2, RegularSize.E20, |
| 44 | RegularSize.E39, RegularSize.E40, "A", LargeSize.E40, null }; |
| 45 | } |
| 46 | }, |
| 47 | LARGE_ENUM_SET { |
| 48 | @Override Set<?> newSet() { |
| 49 | return EnumSet.allOf(LargeSize.class); |
| 50 | } |
| 51 | @Override Object[] testValues() { |
| 52 | return new Object[] { LargeSize.E1, LargeSize.E63, LargeSize.E64, |
| 53 | LargeSize.E65, LargeSize.E140, "A", RegularSize.E40, null }; |
| 54 | } |
| 55 | }; |
| 56 | |
| 57 | abstract Set<?> newSet(); |
| 58 | abstract Object[] testValues(); |
| 59 | } |
| 60 | |
| 61 | enum RegularSize { |
| 62 | E1, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11, E12, E13, E14, E15, E16, E17, |
| 63 | E18, E19, E20, E21, E22, E23, E24, E25, E26, E27, E28, E29, E30, E31, E32, |
| 64 | E33, E34, E35, E36, E37, E38, E39, E40, |
| 65 | } |
| 66 | |
| 67 | enum LargeSize { |
| 68 | E1, E2, E3, E4, E5, E6, E7, E8, E9, E10, E11, E12, E13, E14, E15, E16, E17, |
| 69 | E18, E19, E20, E21, E22, E23, E24, E25, E26, E27, E28, E29, E30, E31, E32, |
| 70 | E33, E34, E35, E36, E37, E38, E39, E40, E41, E42, E43, E44, E45, E46, E47, |
| 71 | E48, E49, E50, E51, E52, E53, E54, E55, E56, E57, E58, E59, E60, E61, E62, |
| 72 | E63, E64, E65, E66, E67, E68, E69, E70, E71, E72, E73, E74, E75, E76, E77, |
| 73 | E78, E79, E80, E81, E82, E83, E84, E85, E86, E87, E88, E89, E90, E91, E92, |
| 74 | E93, E94, E95, E96, E97, E98, E99, E100, E101, E102, E103, E104, E105, E106, |
| 75 | E107, E108, E109, E110, E111, E112, E113, E114, E115, E116, E117, E118, |
| 76 | E119, E120, E121, E122, E123, E124, E125, E126, E127, E128, E129, E130, |
| 77 | E131, E132, E133, E134, E135, E136, E137, E138, E139, E140, |
| 78 | } |
| 79 | |
| 80 | private Set<?> set; |
| 81 | private Object[] testValues; |
| 82 | |
| 83 | @Override protected void setUp() throws Exception { |
| 84 | this.set = setMaker.newSet(); |
| 85 | this.testValues = setMaker.testValues(); |
| 86 | } |
| 87 | |
| 88 | class ContainsBenchmark extends Benchmark { |
| 89 | @Override public Object run(int trials) throws Exception { |
| 90 | int count = 0; |
| 91 | for (int i = 0; i < trials; i++) { |
| 92 | for (Object value : testValues) { |
| 93 | count ^= (set.contains(value) ? i : 0); |
| 94 | } |
| 95 | } |
| 96 | return count > 0; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | public static void main(String[] args) { |
| 101 | Runner.main(EnumSetContainsBenchmarkSuite.class, args); |
| 102 | } |
| 103 | } |