Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/micro/org/openjdk/bench/java/lang/StringEncode.java
41161 views
1
/*
2
* Copyright (c) 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.lang;
24
25
import org.openjdk.jmh.annotations.*;
26
import org.openjdk.jmh.infra.Blackhole;
27
28
import java.nio.charset.Charset;
29
import java.util.concurrent.TimeUnit;
30
31
@BenchmarkMode(Mode.AverageTime)
32
@OutputTimeUnit(TimeUnit.NANOSECONDS)
33
@Fork(value = 3, jvmArgs = "-Xmx1g")
34
@Warmup(iterations = 5, time = 2)
35
@Measurement(iterations = 5, time = 3)
36
@State(Scope.Thread)
37
public class StringEncode {
38
39
@BenchmarkMode(Mode.AverageTime)
40
@OutputTimeUnit(TimeUnit.NANOSECONDS)
41
@Fork(value = 3, jvmArgs = "-Xmx1g")
42
@Warmup(iterations = 5, time = 2)
43
@Measurement(iterations = 5, time = 2)
44
@State(Scope.Thread)
45
public static class WithCharset {
46
47
@Param({"US-ASCII", "ISO-8859-1", "UTF-8", "MS932", "ISO-8859-6"})
48
private String charsetName;
49
50
private Charset charset;
51
private String asciiString;
52
private String utf16String;
53
54
@Setup
55
public void setup() {
56
charset = Charset.forName(charsetName);
57
asciiString = "ascii string";
58
utf16String = "UTF-\uFF11\uFF16 string";
59
}
60
61
@Benchmark
62
public void encodeCharsetName(Blackhole bh) throws Exception {
63
bh.consume(asciiString.getBytes(charsetName));
64
bh.consume(utf16String.getBytes(charsetName));
65
}
66
67
@Benchmark
68
public void encodeCharset(Blackhole bh) throws Exception {
69
bh.consume(asciiString.getBytes(charset));
70
bh.consume(utf16String.getBytes(charset));
71
}
72
}
73
74
private String asciiDefaultString;
75
private String utf16DefaultString;
76
77
@Setup
78
public void setup() {
79
asciiDefaultString = "ascii string";
80
utf16DefaultString = "UTF-\uFF11\uFF16 string";
81
}
82
83
@Benchmark
84
public void encodeDefault(Blackhole bh) throws Exception {
85
bh.consume(asciiDefaultString.getBytes());
86
bh.consume(utf16DefaultString.getBytes());
87
}
88
}
89
90