1/* 2 * Copyright (C) 2010 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 17package benchmarks.regression; 18 19import com.google.caliper.Param; 20import com.google.caliper.Runner; 21import com.google.caliper.SimpleBenchmark; 22import java.util.concurrent.atomic.AtomicInteger; 23 24public final class MutableIntBenchmark extends SimpleBenchmark { 25 26 enum Kind { 27 ARRAY() { 28 int[] value = new int[1]; 29 30 @Override void timeCreate(int reps) { 31 for (int i = 0; i < reps; i++) { 32 value = new int[] { 5 }; 33 } 34 } 35 @Override void timeIncrement(int reps) { 36 for (int i = 0; i < reps; i++) { 37 value[0]++; 38 } 39 } 40 @Override int timeGet(int reps) { 41 int sum = 0; 42 for (int i = 0; i < reps; i++) { 43 sum += value[0]; 44 } 45 return sum; 46 } 47 }, 48 ATOMIC() { 49 AtomicInteger value = new AtomicInteger(); 50 51 @Override void timeCreate(int reps) { 52 for (int i = 0; i < reps; i++) { 53 value = new AtomicInteger(5); 54 } 55 } 56 @Override void timeIncrement(int reps) { 57 for (int i = 0; i < reps; i++) { 58 value.incrementAndGet(); 59 } 60 } 61 @Override int timeGet(int reps) { 62 int sum = 0; 63 for (int i = 0; i < reps; i++) { 64 sum += value.intValue(); 65 } 66 return sum; 67 } 68 }; 69 70 abstract void timeCreate(int reps); 71 abstract void timeIncrement(int reps); 72 abstract int timeGet(int reps); 73 } 74 75 @Param Kind kind; 76 77 public void timeCreate(int reps) { 78 kind.timeCreate(reps); 79 } 80 81 public void timeIncrement(int reps) { 82 kind.timeIncrement(reps); 83 } 84 85 public void timeGet(int reps) { 86 kind.timeGet(reps); 87 } 88 89 public static void main(String[] args) { 90 Runner.main(MutableIntBenchmark.class, args); 91 } 92} 93