Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/micro/org/openjdk/bench/java/lang/StringHashCode.java
41161 views
1
/*
2
* Copyright (c) 2014, 2019, 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.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.Scope;
30
import org.openjdk.jmh.annotations.Setup;
31
import org.openjdk.jmh.annotations.State;
32
33
import java.util.concurrent.TimeUnit;
34
35
/**
36
* Performance test of String.hashCode() function
37
*/
38
@BenchmarkMode(Mode.AverageTime)
39
@OutputTimeUnit(TimeUnit.NANOSECONDS)
40
@State(Scope.Thread)
41
public class StringHashCode {
42
43
private String hashcode;
44
private String hashcode0;
45
private String empty;
46
47
@Setup
48
public void setup() {
49
hashcode = "abcdefghijkl";
50
hashcode0 = new String(new char[]{72, 90, 100, 89, 105, 2, 72, 90, 100, 89, 105, 2});
51
empty = new String();
52
}
53
54
/**
55
* Benchmark testing String.hashCode() with a regular 12 char string with
56
* the result possibly cached in String
57
*/
58
@Benchmark
59
public int cached() {
60
return hashcode.hashCode();
61
}
62
63
/**
64
* Benchmark testing String.hashCode() with a 12 char string with the
65
* hashcode = 0 forcing the value to always be recalculated.
66
*/
67
@Benchmark
68
public int notCached() {
69
return hashcode0.hashCode();
70
}
71
72
/**
73
* Benchmark testing String.hashCode() with the empty string. Since the
74
* empty String has hashCode = 0, this value is always recalculated.
75
*/
76
@Benchmark
77
public int empty() {
78
return empty.hashCode();
79
}
80
}
81
82