Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/micro/org/openjdk/bench/java/nio/CharsetEncodeDecode.java
41161 views
1
/*
2
* Copyright (c) 2014, 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.nio;
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.OutputTimeUnit;
29
import org.openjdk.jmh.annotations.Param;
30
import org.openjdk.jmh.annotations.Scope;
31
import org.openjdk.jmh.annotations.Setup;
32
import org.openjdk.jmh.annotations.State;
33
34
import java.nio.ByteBuffer;
35
import java.nio.CharBuffer;
36
import java.nio.charset.CharacterCodingException;
37
import java.nio.charset.Charset;
38
import java.nio.charset.CharsetDecoder;
39
import java.nio.charset.CharsetEncoder;
40
import java.util.concurrent.TimeUnit;
41
42
/**
43
* This benchmark tests the encode/decode loops on different Charsets. It was created from an adhoc benchmark addressing
44
* a performance issue which in the end boiled down to the encode/decode loops. This is the reason for the values on the
45
* char and byte arrays.
46
*/
47
@BenchmarkMode(Mode.AverageTime)
48
@OutputTimeUnit(TimeUnit.MICROSECONDS)
49
@State(Scope.Thread)
50
public class CharsetEncodeDecode {
51
52
private byte[] BYTES;
53
private char[] CHARS;
54
55
private CharsetEncoder encoder;
56
private CharsetDecoder decoder;
57
58
@Param({"BIG5", "ISO-8859-15", "ASCII", "UTF-16"})
59
private String type;
60
61
@Param("16384")
62
private int size;
63
64
@Setup
65
public void prepare() {
66
BYTES = new byte[size];
67
CHARS = new char[size];
68
for (int i = 0; i < size; ++i) {
69
int val = 48 + (i % 16);
70
BYTES[i] = (byte) val;
71
CHARS[i] = (char) val;
72
}
73
74
encoder = Charset.forName(type).newEncoder();
75
decoder = Charset.forName(type).newDecoder();
76
}
77
78
@Benchmark
79
public ByteBuffer encode() throws CharacterCodingException {
80
CharBuffer charBuffer = CharBuffer.wrap(CHARS);
81
return encoder.encode(charBuffer);
82
}
83
84
@Benchmark
85
public CharBuffer decode() throws CharacterCodingException {
86
ByteBuffer byteBuffer = ByteBuffer.wrap(BYTES);
87
return decoder.decode(byteBuffer);
88
}
89
90
}
91
92