forked from ScottOaks/JavaPerformanceTuning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringInternTest.java
More file actions
78 lines (69 loc) · 2.2 KB
/
Copy pathStringInternTest.java
File metadata and controls
78 lines (69 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
* Copyright (c) 2013,2014 Scott Oaks. All rights reserved.
*/
package net.sdo;
import extra166y.CustomConcurrentHashMap;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
public class StringInternTest {
private static final int SIZE = 10000000;
private static CustomConcurrentHashMap<String, String> map =
new CustomConcurrentHashMap<>(CustomConcurrentHashMap.WEAK,
CustomConcurrentHashMap.EQUALS,
CustomConcurrentHashMap.WEAK,
CustomConcurrentHashMap.EQUALS,
1009);
private static ArrayList<String> list = new ArrayList<>(SIZE);
public static String selfIntern(String s) {
String result = map.get(s);
if (result == null) {
result = map.putIfAbsent(s, s);
if (result == null) {
result = s;
}
}
return result;
}
public static void timeSelf() {
for (int i = 0; i < SIZE; i++) {
String s = selfIntern(makeRandomString());
list.add(s);
}
}
public static void timeJDK() {
for (int i = 0; i < SIZE; i++) {
String s = makeRandomString().intern();
list.add(s);
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
long then, now;
switch (args[0]) {
case "self":
then = System.currentTimeMillis();
timeSelf();
now = System.currentTimeMillis();
System.out.println("Self took " + (now - then));
break;
case "jdk":
then = System.currentTimeMillis();
timeJDK();
now = System.currentTimeMillis();
System.out.println("JDK took " + (now - then));
break;
}
}
private static String makeRandomString() {
Random r = ThreadLocalRandom.current();
char[] c = new char[r.nextInt(256) + 1];
for (int i = 0; i < c.length; i++) {
c[i] = (char) ('a' + r.nextInt(26));
}
return new String(c);
}
}