blob: 9e7f8934ce791f6eaca1bb450c959375f89e939b [file] [log] [blame]
Aurimas Liutikas88c7ff12023-08-10 12:42:26 -07001/*
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 java.util.concurrent.atomic.AtomicInteger;
21
22public final class MutableIntBenchmark {
23
24 enum Kind {
25 ARRAY() {
26 int[] value = new int[1];
27
28 @Override void timeCreate(int reps) {
29 for (int i = 0; i < reps; i++) {
30 value = new int[] { 5 };
31 }
32 }
33 @Override void timeIncrement(int reps) {
34 for (int i = 0; i < reps; i++) {
35 value[0]++;
36 }
37 }
38 @Override int timeGet(int reps) {
39 int sum = 0;
40 for (int i = 0; i < reps; i++) {
41 sum += value[0];
42 }
43 return sum;
44 }
45 },
46 ATOMIC() {
47 AtomicInteger value = new AtomicInteger();
48
49 @Override void timeCreate(int reps) {
50 for (int i = 0; i < reps; i++) {
51 value = new AtomicInteger(5);
52 }
53 }
54 @Override void timeIncrement(int reps) {
55 for (int i = 0; i < reps; i++) {
56 value.incrementAndGet();
57 }
58 }
59 @Override int timeGet(int reps) {
60 int sum = 0;
61 for (int i = 0; i < reps; i++) {
62 sum += value.intValue();
63 }
64 return sum;
65 }
66 };
67
68 abstract void timeCreate(int reps);
69 abstract void timeIncrement(int reps);
70 abstract int timeGet(int reps);
71 }
72
73 @Param Kind kind;
74
75 public void timeCreate(int reps) {
76 kind.timeCreate(reps);
77 }
78
79 public void timeIncrement(int reps) {
80 kind.timeIncrement(reps);
81 }
82
83 public void timeGet(int reps) {
84 kind.timeGet(reps);
85 }
86}