Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/micro/org/openjdk/bench/java/math/BigIntegers.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.annotations.Param;
34
import org.openjdk.jmh.infra.Blackhole;
35
36
import java.math.BigInteger;
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 BigIntegers {
44
45
private BigInteger[] hugeArray, largeArray, smallArray, shiftArray, smallShiftArray;
46
public String[] dummyStringArray;
47
public Object[] dummyArr;
48
private static final int TESTSIZE = 1000;
49
50
@Param({"32", "64", "96", "128", "160", "192", "224", "256"})
51
private int maxNumbits;
52
53
@Setup
54
public void setup() {
55
Random r = new Random(1123);
56
int numbits = r.nextInt(16384);
57
58
hugeArray = new BigInteger[TESTSIZE]; /*
59
* Huge numbers larger than
60
* MAX_LONG
61
*/
62
largeArray = new BigInteger[TESTSIZE]; /*
63
* Large numbers less than
64
* MAX_LONG but larger than
65
* MAX_INT
66
*/
67
smallArray = new BigInteger[TESTSIZE]; /*
68
* Small number less than
69
* MAX_INT
70
*/
71
shiftArray = new BigInteger[TESTSIZE]; /*
72
* Each array entry is atmost 16k bits
73
* in size
74
*/
75
smallShiftArray = new BigInteger[TESTSIZE]; /*
76
* Small numbers, bits count in range [maxNumbits - 31, maxNumbits]
77
*/
78
79
dummyStringArray = new String[TESTSIZE];
80
dummyArr = new Object[TESTSIZE];
81
82
for (int i = 0; i < TESTSIZE; i++) {
83
int value = Math.abs(r.nextInt());
84
85
hugeArray[i] = new BigInteger("" + ((long) value + (long) Integer.MAX_VALUE)
86
+ ((long) value + (long) Integer.MAX_VALUE));
87
largeArray[i] = new BigInteger("" + ((long) value + (long) Integer.MAX_VALUE));
88
smallArray[i] = new BigInteger("" + ((long) value / 1000));
89
shiftArray[i] = new BigInteger(numbits, r);
90
smallShiftArray[i] = new BigInteger(Math.max(maxNumbits - value % 32, 0), r);
91
}
92
}
93
94
/** Test BigInteger.toString() with huge numbers larger than MAX_LONG */
95
@Benchmark
96
@OperationsPerInvocation(TESTSIZE)
97
public void testHugeToString(Blackhole bh) {
98
for (BigInteger s : hugeArray) {
99
bh.consume(s.toString());
100
}
101
}
102
103
/** Test BigInteger.toString() with large numbers less than MAX_LONG but larger than MAX_INT */
104
@Benchmark
105
@OperationsPerInvocation(TESTSIZE)
106
public void testLargeToString(Blackhole bh) {
107
for (BigInteger s : largeArray) {
108
bh.consume(s.toString());
109
}
110
}
111
112
/** Test BigInteger.toString() with small numbers less than MAX_INT */
113
@Benchmark
114
@OperationsPerInvocation(TESTSIZE)
115
public void testSmallToString(Blackhole bh) {
116
for (BigInteger s : smallArray) {
117
bh.consume(s.toString());
118
}
119
}
120
121
/** Invokes the multiply method of BigInteger with various different values. */
122
@Benchmark
123
@OperationsPerInvocation(TESTSIZE)
124
public void testMultiply(Blackhole bh) {
125
BigInteger tmp = null;
126
for (BigInteger s : hugeArray) {
127
if (tmp == null) {
128
tmp = s;
129
continue;
130
}
131
tmp = tmp.multiply(s);
132
}
133
bh.consume(tmp);
134
}
135
136
/** Invokes the multiply method of BigInteger with various different values. */
137
@Benchmark
138
@OperationsPerInvocation(TESTSIZE)
139
public void testAdd(Blackhole bh) {
140
BigInteger tmp = null;
141
for (BigInteger s : hugeArray) {
142
if (tmp == null) {
143
tmp = s;
144
continue;
145
}
146
tmp = tmp.add(s);
147
}
148
bh.consume(tmp);
149
}
150
151
/** Invokes the shiftLeft method of BigInteger with different values. */
152
@Benchmark
153
@OperationsPerInvocation(TESTSIZE)
154
public void testLeftShift(Blackhole bh) {
155
Random rand = new Random();
156
int shift = rand.nextInt(30) + 1;
157
BigInteger tmp = null;
158
for (BigInteger s : shiftArray) {
159
if (tmp == null) {
160
tmp = s;
161
continue;
162
}
163
tmp = tmp.shiftLeft(shift);
164
}
165
bh.consume(tmp);
166
}
167
168
/** Invokes the shiftRight method of BigInteger with different values. */
169
@Benchmark
170
@OperationsPerInvocation(TESTSIZE)
171
public void testRightShift(Blackhole bh) {
172
Random rand = new Random();
173
int shift = rand.nextInt(30) + 1;
174
BigInteger tmp = null;
175
for (BigInteger s : shiftArray) {
176
if (tmp == null) {
177
tmp = s;
178
continue;
179
}
180
tmp = tmp.shiftRight(shift);
181
}
182
bh.consume(tmp);
183
}
184
185
/** Invokes the shiftLeft method of small BigInteger with different values. */
186
@Benchmark
187
@OperationsPerInvocation(TESTSIZE)
188
public void testSmallLeftShift(Blackhole bh) {
189
Random rand = new Random();
190
int shift = rand.nextInt(30) + 1;
191
BigInteger tmp = null;
192
for (BigInteger s : smallShiftArray) {
193
tmp = s.shiftLeft(shift);
194
bh.consume(tmp);
195
}
196
}
197
198
/** Invokes the shiftRight method of small BigInteger with different values. */
199
@Benchmark
200
@OperationsPerInvocation(TESTSIZE)
201
public void testSmallRightShift(Blackhole bh) {
202
Random rand = new Random();
203
int shift = rand.nextInt(30) + 1;
204
BigInteger tmp = null;
205
for (BigInteger s : smallShiftArray) {
206
tmp = s.shiftRight(shift);
207
bh.consume(tmp);
208
}
209
}
210
}
211
212