3 public int compare(
final Map.Entry<Method, Long> o1,
final Map.Entry<Method, Long> o2) {
4 final long d = o1.getValue() - o2.getValue();
5 return d < 0 ? -1 : d > 0 ? +2 : o1.getKey().getName().compareTo(o2.getKey().getName());
13 public static Comparator<Map.Entry<Method, Long>>
getInstance() {
25 public static int sum;
27 private static Pattern
lineSplit = Pattern.compile(
"\n");
29 private static Pattern
wordSplit = Pattern.compile(
"\\s+");
31 public static void main(
final String... args)
throws Throwable {
33 final List<Method> performanceTestMethods =
new ArrayList<Method>();
35 if (method.getAnnotation(
PerfTest.class) !=
null) {
36 performanceTestMethods.add(method);
39 Collections.shuffle(performanceTestMethods);
40 final Map<Method, Long> results =
new HashMap<Method, Long>();
41 final List<Integer> sums =
new ArrayList<Integer>();
42 for (
final Method performanceTestMethod : performanceTestMethods) {
44 performanceTestMethod.invoke(
null);
45 final long start = System.currentTimeMillis();
47 performanceTestMethod.invoke(
null);
50 final long end = System.currentTimeMillis();
51 final long time = end - start;
52 results.put(performanceTestMethod, time);
56 sortedResults.addAll(results.entrySet());
57 for (
final Map.Entry<Method, Long> result : sortedResults) {
58 final Method performanceTestMethod = result.getKey();
59 final long time = result.getValue();
60 System.out.printf(
"%24s %05dms %s%n", performanceTestMethod.getName(), time, performanceTestMethod.getAnnotation(
PerfTest.class).value());
65 @
PerfTest(
"BufferedReader, regionMatches (old)")
66 public static
void old() throws IOException {
67 final BufferedReader in =
new BufferedReader(
new InputStreamReader(
new FileInputStream(
testFileName),
"iso-8859-1"));
70 for (String line; (line = in.readLine()) !=
null; ) {
72 if (line.regionMatches(0,
"arch ", 0, 5)) {
74 }
else if (line.regionMatches(0,
"endmsg", 0, 6)) {
76 }
else if (line.regionMatches(0,
"end", 0, 3)) {
78 }
else if (line.regionMatches(0,
"msg", 0, 3)) {
80 }
else if (line.regionMatches(0,
"x ", 0, 2)) {
82 }
else if (line.regionMatches(0,
"y ", 0, 2)) {
84 }
else if (line.regionMatches(0,
"type ", 0, 5)) {
86 }
else if (line.regionMatches(0,
"direction ", 0, 10)) {
88 }
else if (line.regionMatches(0,
"face ", 0, 5)) {
98 @
PerfTest(
"BufferedReader, startsWith (current)")
99 public static
void current() throws IOException {
100 final BufferedReader in =
new BufferedReader(
new InputStreamReader(
new FileInputStream(
testFileName),
"iso-8859-1"));
103 for (String line; (line = in.readLine()) !=
null; ) {
105 if (line.startsWith(
"arch ")) {
107 }
else if (line.startsWith(
"endmsg")) {
109 }
else if (line.startsWith(
"end")) {
111 }
else if (line.startsWith(
"msg")) {
113 }
else if (line.startsWith(
"x ")) {
115 }
else if (line.startsWith(
"y ")) {
117 }
else if (line.startsWith(
"type ")) {
119 }
else if (line.startsWith(
"direction ")) {
121 }
else if (line.startsWith(
"face ")) {
131 @
PerfTest(
"BufferedReader, word by regex split of java.lang.String")
133 final BufferedReader in =
new BufferedReader(
new InputStreamReader(
new FileInputStream(
testFileName),
"iso-8859-1"));
136 for (String line; (line = in.readLine()) !=
null; ) {
138 final String word = line.split(
"\\s+")[0];
139 if (
"arch".equals(word)) {
141 }
else if (
"endmsg".equals(word)) {
143 }
else if (
"end".equals(word)) {
145 }
else if (
"msg".equals(word)) {
147 }
else if (
"x".equals(word)) {
149 }
else if (
"y".equals(word)) {
151 }
else if (
"type".equals(word)) {
153 }
else if (
"direction".equals(word)) {
155 }
else if (
"face".equals(word)) {
165 @
PerfTest(
"BufferedReader, word by regex split of java.util.regex.Pattern")
167 final BufferedReader in =
new BufferedReader(
new InputStreamReader(
new FileInputStream(
testFileName),
"iso-8859-1"));
170 for (String line; (line = in.readLine()) !=
null; ) {
172 final String word =
wordSplit.split(line)[0];
173 if (
"arch".equals(word)) {
175 }
else if (
"endmsg".equals(word)) {
177 }
else if (
"end".equals(word)) {
179 }
else if (
"msg".equals(word)) {
181 }
else if (
"x".equals(word)) {
183 }
else if (
"y".equals(word)) {
185 }
else if (
"type".equals(word)) {
187 }
else if (
"direction".equals(word)) {
189 }
else if (
"face".equals(word)) {
199 @
PerfTest(
"BufferedReader, manual word split using String.indexOf(' ')")
201 final BufferedReader in =
new BufferedReader(
new InputStreamReader(
new FileInputStream(
testFileName),
"iso-8859-1"));
204 for (String line; (line = in.readLine()) !=
null; ) {
206 final int indexOfSpace = line.indexOf(
' ');
207 final String word = indexOfSpace > 0 ? line.substring(0, indexOfSpace) : line;
208 if (
"arch".equals(word)) {
210 }
else if (
"endmsg".equals(word)) {
212 }
else if (
"end".equals(word)) {
214 }
else if (
"msg".equals(word)) {
216 }
else if (
"x".equals(word)) {
218 }
else if (
"y".equals(word)) {
220 }
else if (
"type".equals(word)) {
222 }
else if (
"direction".equals(word)) {
224 }
else if (
"face".equals(word)) {
234 @
PerfTest(
"BufferedReader, manual word split using String.indexOf(' ') using hashCode")
236 final BufferedReader in =
new BufferedReader(
new InputStreamReader(
new FileInputStream(
testFileName),
"iso-8859-1"));
239 for (String line; (line = in.readLine()) !=
null; ) {
241 final int indexOfSpace = line.indexOf(
' ');
242 final String word = indexOfSpace > 0 ? line.substring(0, indexOfSpace) : line;
243 final int hash = word.hashCode();
244 if (
"arch".hashCode() == hash) {
246 }
else if (
"endmsg".hashCode() == hash) {
248 }
else if (
"end".hashCode() == hash) {
250 }
else if (
"msg".hashCode() == hash) {
252 }
else if (
"x".hashCode() == hash) {
254 }
else if (
"y".hashCode() == hash) {
256 }
else if (
"type".hashCode() == hash) {
258 }
else if (
"direction".hashCode() == hash) {
260 }
else if (
"face".hashCode() == hash) {
270 private static final int hashArch =
"arch".hashCode();
274 private static final int hashEnd =
"end".hashCode();
276 private static final int hashMsg =
"msg".hashCode();
278 private static final int hashX =
"x".hashCode();
280 private static final int hashY =
"y".hashCode();
282 private static final int hashType =
"type".hashCode();
286 private static final int hashFace =
"face".hashCode();
290 @
PerfTest(
"BufferedReader, manual word split using String.indexOf(' ') using a cached hashCode")
292 final BufferedReader in =
new BufferedReader(
new InputStreamReader(
new FileInputStream(
testFileName),
"iso-8859-1"));
295 for (String line; (line = in.readLine()) !=
null; ) {
297 final int indexOfSpace = line.indexOf(
' ');
298 final String word = indexOfSpace > 0 ? line.substring(0, indexOfSpace) : line;
299 final int hash = word.hashCode();
308 }
else if (hash ==
hashX) {
310 }
else if (hash ==
hashY) {
326 @
PerfTest(
"BufferedReader, manual word split using String.indexOf(' ') using a cached hashCode via switch")
328 final BufferedReader in =
new BufferedReader(
new InputStreamReader(
new FileInputStream(
testFileName),
"iso-8859-1"));
331 for (String line; (line = in.readLine()) !=
null; ) {
333 final int indexOfSpace = line.indexOf(
' ');
334 final String word = indexOfSpace > 0 ? line.substring(0, indexOfSpace) : line;
335 final int hash = word.hashCode();
337 for (hashIndex = 0; hashIndex <
hashes.length; hashIndex++) {
338 if (
hashes[hashIndex] == hash) {
786 @
PerfTest(
"New File I/O nondirect -> String, manual split of java.lang.String")
789 final FileChannel fileChannel =
new FileInputStream(
testFileName).getChannel();
790 final ByteBuffer buffer = ByteBuffer.allocate((
int) fileChannel.size());
791 fileChannel.read(buffer);
793 final String file =
new String(buffer.array(), 0);
794 for (String line :
lineSplit.split(file)) {
796 final int indexOfSpace = line.indexOf(
' ');
797 final String word = indexOfSpace > 0 ? line.substring(0, indexOfSpace) : line;
798 if (
"arch".equals(word)) {
800 }
else if (
"endmsg".equals(word)) {
802 }
else if (
"end".equals(word)) {
804 }
else if (
"msg".equals(word)) {
806 }
else if (
"x".equals(word)) {
808 }
else if (
"y".equals(word)) {
810 }
else if (
"type".equals(word)) {
812 }
else if (
"direction".equals(word)) {
814 }
else if (
"face".equals(word)) {
824 private static final CharsetDecoder
decoder = Charset.forName(
"iso-8859-1").newDecoder();
826 @
PerfTest(
"Mapped File I/O nondirect -> charset, pattern split")
827 public static
void nioChar() throws IOException {
829 final FileChannel fileChannel =
new FileInputStream(
testFileName).getChannel();
830 final ByteBuffer buffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size());
831 final CharBuffer charBuffer =
decoder.decode(buffer);
833 for (String line :
lineSplit.split(charBuffer)) {
835 final int indexOfSpace = line.indexOf(
' ');
836 final String word = indexOfSpace > 0 ? line.substring(0, indexOfSpace) : line;
837 if (
"arch".equals(word)) {
839 }
else if (
"endmsg".equals(word)) {
841 }
else if (
"end".equals(word)) {
843 }
else if (
"msg".equals(word)) {
845 }
else if (
"x".equals(word)) {
847 }
else if (
"y".equals(word)) {
849 }
else if (
"type".equals(word)) {
851 }
else if (
"direction".equals(word)) {
853 }
else if (
"face".equals(word)) {
863 @
PerfTest(
"BufferdReader -> StringBuilder, manual split of java.lang.String")
866 final BufferedReader in =
new BufferedReader(
new InputStreamReader(
new FileInputStream(
testFileName),
"iso-8859-1"), 8192);
868 final StringBuilder file =
new StringBuilder();
869 final char[] buf =
new char[8192];
870 for (
int charsRead; (charsRead = in.read(buf)) != -1; ) {
871 file.append(buf, 0, charsRead);
873 for (String line :
lineSplit.split(file)) {
875 final int indexOfSpace = line.indexOf(
' ');
876 final String word = indexOfSpace > 0 ? line.substring(0, indexOfSpace) : line;
877 if (
"arch".equals(word)) {
879 }
else if (
"endmsg".equals(word)) {
881 }
else if (
"end".equals(word)) {
883 }
else if (
"msg".equals(word)) {
885 }
else if (
"x".equals(word)) {
887 }
else if (
"y".equals(word)) {
889 }
else if (
"type".equals(word)) {
891 }
else if (
"direction".equals(word)) {
893 }
else if (
"face".equals(word)) {
903 @
PerfTest(
"BufferedInputStream -> String, manual split")
907 final byte[] buf =
new byte[size];
908 final BufferedInputStream in =
new BufferedInputStream(
new FileInputStream(
testFileName));
910 for (
int pos = 0, bytesRead; pos < size && (bytesRead = in.read(buf, pos, size - pos)) != -1; pos += bytesRead) {
912 final String file =
new String(buf);
913 final StringTokenizer tk =
new StringTokenizer(file,
"\n");
914 while (tk.hasMoreTokens()) {
915 String line = tk.nextToken();
917 final int indexOfSpace = line.indexOf(
' ');
918 final String word = indexOfSpace > 0 ? line.substring(0, indexOfSpace) : line;
919 if (
"arch".equals(word)) {
921 }
else if (
"endmsg".equals(word)) {
923 }
else if (
"end".equals(word)) {
925 }
else if (
"msg".equals(word)) {
927 }
else if (
"x".equals(word)) {
929 }
else if (
"y".equals(word)) {
931 }
else if (
"type".equals(word)) {
933 }
else if (
"direction".equals(word)) {
935 }
else if (
"face".equals(word)) {
945 @
PerfTest(
"Mapped File I/O nondirect -> String, StringTokenizer")
946 public static
void nioByte() throws IOException {
948 final FileChannel fileChannel =
new FileInputStream(
testFileName).getChannel();
949 final ByteBuffer buffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size());
951 final byte[] buf =
new byte[buffer.remaining()];
953 final String file =
new String(buf);
954 final StringTokenizer tk =
new StringTokenizer(file,
"\n");
955 while (tk.hasMoreTokens()) {
956 String line = tk.nextToken();
958 final int indexOfSpace = line.indexOf(
' ');
959 final String word = indexOfSpace > 0 ? line.substring(0, indexOfSpace) : line;
960 if (
"arch".equals(word)) {
962 }
else if (
"endmsg".equals(word)) {
964 }
else if (
"end".equals(word)) {
966 }
else if (
"msg".equals(word)) {
968 }
else if (
"x".equals(word)) {
970 }
else if (
"y".equals(word)) {
972 }
else if (
"type".equals(word)) {
974 }
else if (
"direction".equals(word)) {
976 }
else if (
"face".equals(word)) {