Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/micro/org/openjdk/bench/java/util/UUIDBench.java
41161 views
1
/*
2
* Copyright (c) 2020, 2021, 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.util;
24
25
import java.util.UUID;
26
import java.util.concurrent.TimeUnit;
27
28
import org.openjdk.jmh.annotations.*;
29
30
@State(Scope.Thread)
31
@OutputTimeUnit(TimeUnit.MICROSECONDS)
32
@Fork(value = 3)
33
@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
34
@Measurement(iterations = 5, time = 2, timeUnit = TimeUnit.SECONDS)
35
public class UUIDBench {
36
37
@Param("20000")
38
private int size;
39
40
private byte[][] uuidBytes;
41
42
private UUID[] uuids;
43
44
private String[] uuidStrings;
45
46
private int index = 0;
47
48
@Setup
49
public void setup() {
50
uuidBytes = new byte[size][];
51
uuids = new UUID[size];
52
uuidStrings = new String[size];
53
java.util.Random r = new java.util.Random(0);
54
for (int i = 0; i < this.uuidStrings.length; i++) {
55
final UUID uuid = UUID.randomUUID();
56
this.uuidBytes[i] = new byte[16];
57
r.nextBytes(this.uuidBytes[i]);
58
this.uuids[i] = uuid;
59
this.uuidStrings[i] = uuid.toString();
60
}
61
}
62
63
@Setup(Level.Iteration)
64
public void setupIteration() {
65
index++;
66
if (index >= size) {
67
index = 0;
68
}
69
}
70
71
@Benchmark
72
public UUID fromString() {
73
return UUID.fromString(uuidStrings[index]);
74
}
75
76
@Benchmark
77
public String toString() {
78
return uuids[index].toString();
79
}
80
81
@Benchmark
82
public UUID fromType3Bytes() {
83
return UUID.nameUUIDFromBytes(uuidBytes[index]);
84
}
85
}
86
87