Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/micro/org/openjdk/bench/java/math/BigDecimals.java
41161 views
1
/*
2
* Copyright (c) 2014, 2020, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
package org.openjdk.bench.java.math;
24
25
import org.openjdk.jmh.annotations.Benchmark;
26
import org.openjdk.jmh.annotations.BenchmarkMode;
27
import org.openjdk.jmh.annotations.Mode;
28
import org.openjdk.jmh.annotations.OperationsPerInvocation;
29
import org.openjdk.jmh.annotations.OutputTimeUnit;
30
import org.openjdk.jmh.annotations.Scope;
31
import org.openjdk.jmh.annotations.Setup;
32
import org.openjdk.jmh.annotations.State;
33
import org.openjdk.jmh.infra.Blackhole;
34
35
import java.math.BigDecimal;
36
import java.math.RoundingMode;
37
import java.util.Random;
38
import java.util.concurrent.TimeUnit;
39
40
@BenchmarkMode(Mode.AverageTime)
41
@OutputTimeUnit(TimeUnit.NANOSECONDS)
42
@State(Scope.Thread)
43
public class BigDecimals {
44
45
/** Make sure TEST_SIZE is used to size the arrays. We need this constant to parametrize the operations count. */
46
private static final int TEST_SIZE = 100;
47
48
/* dummy variables for intermediate results */
49
public Object[] dummyArr;
50
public String[] dummyStringArray;
51
public int dummy;
52
53
/* array to hold the created objects. */
54
private BigDecimal[] bigDecimals;
55
private String[] stringInputs;
56
private double[] doubleInputs;
57
private BigDecimal[] hugeArray, largeArray, smallArray;
58
59
@Setup
60
public void setup() {
61
Random r = new Random(1123);
62
dummyArr = new Object[TEST_SIZE];
63
bigDecimals = new BigDecimal[TEST_SIZE];
64
stringInputs = new String[TEST_SIZE];
65
doubleInputs = new double[TEST_SIZE];
66
for (int i = 0; i < TEST_SIZE; i++) {
67
double value = (double) (i + 1);
68
switch (i % 4) {
69
case 0:
70
value = -value * 54345.0d;
71
break;
72
case 1:
73
value = value * 5434543453454355e100;
74
break;
75
case 2:
76
value = -value / 5434543453454355e100;
77
break;
78
case 3:
79
break;
80
}
81
82
bigDecimals[i] = new BigDecimal(value);
83
stringInputs[i] = "" + value;
84
doubleInputs[i] = value;
85
}
86
87
/*
88
* Huge numbers larger than MAX_LONG
89
*/
90
hugeArray = new BigDecimal[TEST_SIZE];
91
92
/*
93
* Large numbers less than MAX_LONG but larger than MAX_INT
94
*/
95
largeArray = new BigDecimal[TEST_SIZE];
96
97
/*
98
* Small number less than MAX_INT
99
*/
100
smallArray = new BigDecimal[TEST_SIZE];
101
102
dummyStringArray = new String[TEST_SIZE];
103
for (int i = 0; i < TEST_SIZE; i++) {
104
int value = Math.abs(r.nextInt());
105
hugeArray[i] = new BigDecimal("" + ((long) value + (long) Integer.MAX_VALUE)
106
+ ((long) value + (long) Integer.MAX_VALUE) + ".55");
107
largeArray[i] = new BigDecimal("" + ((long) value + (long) Integer.MAX_VALUE) + ".55");
108
smallArray[i] = new BigDecimal("" + ((long) value / 1000) + ".55");
109
}
110
}
111
112
/** Invokes the (String)-constructor of BigDecimal with various different values. */
113
@Benchmark
114
@OperationsPerInvocation(TEST_SIZE)
115
public void testConstructorWithString(Blackhole bh) {
116
for (String s : stringInputs) {
117
bh.consume(new BigDecimal(s));
118
}
119
}
120
121
/** Invokes the (double)-constructor of BigDecimal with various different values. */
122
@Benchmark
123
@OperationsPerInvocation(TEST_SIZE)
124
public void testConstructorWithDouble(Blackhole bh) {
125
for (double s : doubleInputs) {
126
bh.consume(new BigDecimal(s));
127
}
128
}
129
130
/** Invokes the toString method of BigDecimal with various different values. */
131
@Benchmark
132
@OperationsPerInvocation(TEST_SIZE)
133
public void testToString(Blackhole bh) {
134
for (BigDecimal s : bigDecimals) {
135
bh.consume(s.toString());
136
}
137
}
138
139
/**
140
* Invokes the setScale method of BigDecimal with various different values.
141
*/
142
@Benchmark
143
@OperationsPerInvocation(TEST_SIZE)
144
public void testSetScale(Blackhole bh) {
145
for (BigDecimal s : bigDecimals) {
146
bh.consume(s.setScale(2, RoundingMode.HALF_UP));
147
}
148
}
149
150
/** Invokes the setScale method of BigDecimal with various different values. */
151
@Benchmark
152
@OperationsPerInvocation(50 * TEST_SIZE)
153
public void testSetScaleVarious(Blackhole bh) {
154
for (int scale = 0; scale < 50; scale++) {
155
for (BigDecimal s : bigDecimals) {
156
bh.consume(s.setScale(scale, RoundingMode.HALF_UP));
157
}
158
}
159
}
160
161
/** Invokes the add method of BigDecimal with various different values. */
162
@Benchmark
163
@OperationsPerInvocation(TEST_SIZE)
164
public void testAdd(Blackhole bh) {
165
BigDecimal tmp = null;
166
for (BigDecimal s : bigDecimals) {
167
if (tmp == null) {
168
tmp = s;
169
continue;
170
}
171
tmp = tmp.add(s);
172
}
173
bh.consume(tmp);
174
}
175
176
/** Invokes the multiply method of BigDecimal with various different values. */
177
@Benchmark
178
@OperationsPerInvocation(TEST_SIZE)
179
public void testMultiply(Blackhole bh) {
180
BigDecimal tmp = null;
181
for (BigDecimal s : bigDecimals) {
182
if (tmp == null) {
183
tmp = s;
184
continue;
185
}
186
tmp = tmp.multiply(s);
187
}
188
bh.consume(tmp);
189
}
190
191
/** Invokes the compareTo method of BigDecimal with various different values. */
192
@Benchmark
193
@OperationsPerInvocation(TEST_SIZE - 1)
194
public void testCompareTo(Blackhole bh) {
195
BigDecimal c = bigDecimals[0];
196
for (BigDecimal s : bigDecimals) {
197
bh.consume(c.compareTo(s));
198
}
199
}
200
201
/** Test BigDecimal.toString() with huge numbers larger than MAX_LONG */
202
@Benchmark
203
@OperationsPerInvocation(TEST_SIZE)
204
public void testHugeToString(Blackhole bh) {
205
for (BigDecimal s : hugeArray) {
206
bh.consume(s.toString());
207
}
208
}
209
210
/** Test BigDecimal.toString() with large numbers less than MAX_LONG but larger than MAX_INT */
211
@Benchmark
212
@OperationsPerInvocation(TEST_SIZE)
213
public void testLargeToString(Blackhole bh) {
214
for (BigDecimal s : largeArray) {
215
bh.consume(s.toString());
216
}
217
}
218
219
/** Test BigDecimal.toString() with small numbers less than MAX_INT */
220
@Benchmark
221
@OperationsPerInvocation(TEST_SIZE)
222
public void testSmallToString(Blackhole bh) {
223
for (BigDecimal s : smallArray) {
224
bh.consume(s.toString());
225
}
226
}
227
}
228
229