Gridarta Editor
PerfTestMapDecode.java
Go to the documentation of this file.
1 class LongValueComparator implements Comparator<Map.Entry<Method, Long>> {
2 
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());
6  }
7 
8  private LongValueComparator() {
9  }
10 
12 
13  public static Comparator<Map.Entry<Method, Long>> getInstance() {
14  return instance;
15  }
16 
17 }
18 
19 public class PerfTestMapDecode {
20 
21  public static final int NUM_OF_TEST_REPETITIONS = 200;
22 
23  private static String testFileName;
24 
25  public static int sum;
26 
27  private static Pattern lineSplit = Pattern.compile("\n");
28 
29  private static Pattern wordSplit = Pattern.compile("\\s+");
30 
31  public static void main(final String... args) throws Throwable {
32  testFileName = args[0];
33  final List<Method> performanceTestMethods = new ArrayList<Method>();
34  for (final Method method : PerfTestMapDecode.class.getMethods()) {
35  if (method.getAnnotation(PerfTest.class) != null) {
36  performanceTestMethods.add(method);
37  }
38  }
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) {
43  sum = 0;
44  performanceTestMethod.invoke(null);
45  final long start = System.currentTimeMillis();
46  for (int i = 0; i < NUM_OF_TEST_REPETITIONS; i++) {
47  performanceTestMethod.invoke(null);
48  }
49  sums.add(sum);
50  final long end = System.currentTimeMillis();
51  final long time = end - start;
52  results.put(performanceTestMethod, time);
53  //System.out.println(sum + " " + performanceTestMethod);
54  }
55  final SortedSet<Map.Entry<Method, Long>> sortedResults = new TreeSet<Map.Entry<Method, Long>>(LongValueComparator.getInstance());
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());
61  }
62  //System.out.println(sums);
63  }
64 
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"));
68  int state = 0;
69  try {
70  for (String line; (line = in.readLine()) != null; ) {
71  line = line.trim();
72  if (line.regionMatches(0, "arch ", 0, 5)) {
73  state += 1;
74  } else if (line.regionMatches(0, "endmsg", 0, 6)) {
75  state += 2;
76  } else if (line.regionMatches(0, "end", 0, 3)) {
77  state += 3;
78  } else if (line.regionMatches(0, "msg", 0, 3)) {
79  state += 4;
80  } else if (line.regionMatches(0, "x ", 0, 2)) {
81  state += 5;
82  } else if (line.regionMatches(0, "y ", 0, 2)) {
83  state += 6;
84  } else if (line.regionMatches(0, "type ", 0, 5)) {
85  state += 7;
86  } else if (line.regionMatches(0, "direction ", 0, 10)) {
87  state += 8;
88  } else if (line.regionMatches(0, "face ", 0, 5)) {
89  state += 9;
90  }
91  }
92  } finally {
93  in.close();
94  }
95  sum += state;
96  }
97 
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"));
101  int state = 0;
102  try {
103  for (String line; (line = in.readLine()) != null; ) {
104  line = line.trim();
105  if (line.startsWith("arch ")) {
106  state += 1;
107  } else if (line.startsWith("endmsg")) {
108  state += 2;
109  } else if (line.startsWith("end")) {
110  state += 3;
111  } else if (line.startsWith("msg")) {
112  state += 4;
113  } else if (line.startsWith("x ")) {
114  state += 5;
115  } else if (line.startsWith("y ")) {
116  state += 6;
117  } else if (line.startsWith("type ")) {
118  state += 7;
119  } else if (line.startsWith("direction ")) {
120  state += 8;
121  } else if (line.startsWith("face ")) {
122  state += 9;
123  }
124  }
125  } finally {
126  in.close();
127  }
128  sum += state;
129  }
130 
131  @PerfTest("BufferedReader, word by regex split of java.lang.String")
132  public static void splitEqualsString() throws IOException {
133  final BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(testFileName), "iso-8859-1"));
134  int state = 0;
135  try {
136  for (String line; (line = in.readLine()) != null; ) {
137  line = line.trim();
138  final String word = line.split("\\s+")[0];
139  if ("arch".equals(word)) {
140  state += 1;
141  } else if ("endmsg".equals(word)) {
142  state += 2;
143  } else if ("end".equals(word)) {
144  state += 3;
145  } else if ("msg".equals(word)) {
146  state += 4;
147  } else if ("x".equals(word)) {
148  state += 5;
149  } else if ("y".equals(word)) {
150  state += 6;
151  } else if ("type".equals(word)) {
152  state += 7;
153  } else if ("direction".equals(word)) {
154  state += 8;
155  } else if ("face".equals(word)) {
156  state += 9;
157  }
158  }
159  } finally {
160  in.close();
161  }
162  sum += state;
163  }
164 
165  @PerfTest("BufferedReader, word by regex split of java.util.regex.Pattern")
166  public static void splitEqualsPattern() throws IOException {
167  final BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(testFileName), "iso-8859-1"));
168  int state = 0;
169  try {
170  for (String line; (line = in.readLine()) != null; ) {
171  line = line.trim();
172  final String word = wordSplit.split(line)[0];
173  if ("arch".equals(word)) {
174  state += 1;
175  } else if ("endmsg".equals(word)) {
176  state += 2;
177  } else if ("end".equals(word)) {
178  state += 3;
179  } else if ("msg".equals(word)) {
180  state += 4;
181  } else if ("x".equals(word)) {
182  state += 5;
183  } else if ("y".equals(word)) {
184  state += 6;
185  } else if ("type".equals(word)) {
186  state += 7;
187  } else if ("direction".equals(word)) {
188  state += 8;
189  } else if ("face".equals(word)) {
190  state += 9;
191  }
192  }
193  } finally {
194  in.close();
195  }
196  sum += state;
197  }
198 
199  @PerfTest("BufferedReader, manual word split using String.indexOf(' ')")
200  public static void manualEquals() throws IOException {
201  final BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(testFileName), "iso-8859-1"));
202  int state = 0;
203  try {
204  for (String line; (line = in.readLine()) != null; ) {
205  line = line.trim();
206  final int indexOfSpace = line.indexOf(' ');
207  final String word = indexOfSpace > 0 ? line.substring(0, indexOfSpace) : line;
208  if ("arch".equals(word)) {
209  state += 1;
210  } else if ("endmsg".equals(word)) {
211  state += 2;
212  } else if ("end".equals(word)) {
213  state += 3;
214  } else if ("msg".equals(word)) {
215  state += 4;
216  } else if ("x".equals(word)) {
217  state += 5;
218  } else if ("y".equals(word)) {
219  state += 6;
220  } else if ("type".equals(word)) {
221  state += 7;
222  } else if ("direction".equals(word)) {
223  state += 8;
224  } else if ("face".equals(word)) {
225  state += 9;
226  }
227  }
228  } finally {
229  in.close();
230  }
231  sum += state;
232  }
233 
234  @PerfTest("BufferedReader, manual word split using String.indexOf(' ') using hashCode")
235  public static void manualEqualsHash() throws IOException {
236  final BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(testFileName), "iso-8859-1"));
237  int state = 0;
238  try {
239  for (String line; (line = in.readLine()) != null; ) {
240  line = line.trim();
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) {
245  state += 1;
246  } else if ("endmsg".hashCode() == hash) {
247  state += 2;
248  } else if ("end".hashCode() == hash) {
249  state += 3;
250  } else if ("msg".hashCode() == hash) {
251  state += 4;
252  } else if ("x".hashCode() == hash) {
253  state += 5;
254  } else if ("y".hashCode() == hash) {
255  state += 6;
256  } else if ("type".hashCode() == hash) {
257  state += 7;
258  } else if ("direction".hashCode() == hash) {
259  state += 8;
260  } else if ("face".hashCode() == hash) {
261  state += 9;
262  }
263  }
264  } finally {
265  in.close();
266  }
267  sum += state;
268  }
269 
270  private static final int hashArch = "arch".hashCode();
271 
272  private static final int hashEndmsg = "endmsg".hashCode();
273 
274  private static final int hashEnd = "end".hashCode();
275 
276  private static final int hashMsg = "msg".hashCode();
277 
278  private static final int hashX = "x".hashCode();
279 
280  private static final int hashY = "y".hashCode();
281 
282  private static final int hashType = "type".hashCode();
283 
284  private static final int hashDirection = "direction".hashCode();
285 
286  private static final int hashFace = "face".hashCode();
287 
288  private static final int[] hashes = { hashArch, hashEndmsg, hashEnd, hashMsg, hashX, hashY, hashType, hashDirection, hashFace };
289 
290  @PerfTest("BufferedReader, manual word split using String.indexOf(' ') using a cached hashCode")
291  public static void manualEqualsCashedHash() throws IOException {
292  final BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(testFileName), "iso-8859-1"));
293  int state = 0;
294  try {
295  for (String line; (line = in.readLine()) != null; ) {
296  line = line.trim();
297  final int indexOfSpace = line.indexOf(' ');
298  final String word = indexOfSpace > 0 ? line.substring(0, indexOfSpace) : line;
299  final int hash = word.hashCode();
300  if (hash == hashArch) {
301  state += 1;
302  } else if (hash == hashEndmsg) {
303  state += 2;
304  } else if (hash == hashEnd) {
305  state += 3;
306  } else if (hash == hashMsg) {
307  state += 4;
308  } else if (hash == hashX) {
309  state += 5;
310  } else if (hash == hashY) {
311  state += 6;
312  } else if (hash == hashType) {
313  state += 7;
314  } else if (hash == hashDirection) {
315  state += 8;
316  } else if (hash == hashFace) {
317  state += 9;
318  }
319  }
320  } finally {
321  in.close();
322  }
323  sum += state;
324  }
325 
326  @PerfTest("BufferedReader, manual word split using String.indexOf(' ') using a cached hashCode via switch")
327  public static void manualEqualsCashedHashSwitch() throws IOException {
328  final BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(testFileName), "iso-8859-1"));
329  int state = 0;
330  try {
331  for (String line; (line = in.readLine()) != null; ) {
332  line = line.trim();
333  final int indexOfSpace = line.indexOf(' ');
334  final String word = indexOfSpace > 0 ? line.substring(0, indexOfSpace) : line;
335  final int hash = word.hashCode();
336  int hashIndex;
337  for (hashIndex = 0; hashIndex < hashes.length; hashIndex++) {
338  if (hashes[hashIndex] == hash) {
339  break;
340  }
341  }
342  switch (hashIndex) {
343  case 0:
344  state += 1;
345  break;
346  case 1:
347  state += 2;
348  break;
349  case 2:
350  state += 3;
351  break;
352  case 3:
353  state += 4;
354  break;
355  case 4:
356  state += 5;
357  break;
358  case 5:
359  state += 6;
360  break;
361  case 6:
362  state += 7;
363  break;
364  case 7:
365  state += 8;
366  break;
367  case 8:
368  state += 9;
369  break;
370  }
371  }
372  } finally {
373  in.close();
374  }
375  sum += state;
376  }
377 
378  /*@PerfTest("RandomAccessFile, startsWith")
379  public static void currentRA() throws IOException {
380  final RandomAccessFile in = new RandomAccessFile(testFileName, "r");
381  int state = 0;
382  try {
383  for (String line; (line = in.readLine()) != null;) {
384  line = line.trim();
385  if (line.startsWith("arch ")) {
386  state += 1;
387  } else if (line.startsWith("endmsg")) {
388  state += 2;
389  } else if (line.startsWith("end")) {
390  state += 3;
391  } else if (line.startsWith("msg")) {
392  state += 4;
393  } else if (line.startsWith("x ")) {
394  state += 5;
395  } else if (line.startsWith("y ")) {
396  state += 6;
397  } else if (line.startsWith("type ")) {
398  state += 7;
399  } else if (line.startsWith("direction ")) {
400  state += 8;
401  } else if (line.startsWith("face ")) {
402  state += 9;
403  }
404  }
405  } finally {
406  in.close();
407  }
408  sum += state;
409  }*/
410 
411  /*@PerfTest("RandomAccessFile, word by regex split of java.lang.String")
412  public static void splitEqualsStringRA() throws IOException {
413  final RandomAccessFile in = new RandomAccessFile(testFileName, "r");
414  int state = 0;
415  try {
416  for (String line; (line = in.readLine()) != null;) {
417  line = line.trim();
418  final String word = line.split("\\s+")[0];
419  if ("arch".equals(word)) {
420  state += 1;
421  } else if ("endmsg".equals(word)) {
422  state += 2;
423  } else if ("end".equals(word)) {
424  state += 3;
425  } else if ("msg".equals(word)) {
426  state += 4;
427  } else if ("x".equals(word)) {
428  state += 5;
429  } else if ("y".equals(word)) {
430  state += 6;
431  } else if ("type".equals(word)) {
432  state += 7;
433  } else if ("direction".equals(word)) {
434  state += 8;
435  } else if ("face".equals(word)) {
436  state += 9;
437  }
438  }
439  } finally {
440  in.close();
441  }
442  sum += state;
443  }*/
444 
445  /*@PerfTest("RandomAccessFile, word by regex split of java.util.regex.Pattern")
446  public static void splitEqualsPatternRA() throws IOException {
447  final RandomAccessFile in = new RandomAccessFile(testFileName, "r");
448  int state = 0;
449  try {
450  for (String line; (line = in.readLine()) != null;) {
451  line = line.trim();
452  final String word = wordSplit.split(line)[0];
453  if ("arch".equals(word)) {
454  state += 1;
455  } else if ("endmsg".equals(word)) {
456  state += 2;
457  } else if ("end".equals(word)) {
458  state += 3;
459  } else if ("msg".equals(word)) {
460  state += 4;
461  } else if ("x".equals(word)) {
462  state += 5;
463  } else if ("y".equals(word)) {
464  state += 6;
465  } else if ("type".equals(word)) {
466  state += 7;
467  } else if ("direction".equals(word)) {
468  state += 8;
469  } else if ("face".equals(word)) {
470  state += 9;
471  }
472  }
473  } finally {
474  in.close();
475  }
476  sum += state;
477  }*/
478 
479  /*@PerfTest("RandomAccessFile, manual word split using String.indexOf(' ')")
480  public static void manualEqualsRA() throws IOException {
481  final RandomAccessFile in = new RandomAccessFile(testFileName, "r");
482  int state = 0;
483  try {
484  for (String line; (line = in.readLine()) != null;) {
485  line = line.trim();
486  final int indexOfSpace = line.indexOf(' ');
487  final String word = indexOfSpace > 0 ? line.substring(0, indexOfSpace) : line;
488  if ("arch".equals(word)) {
489  state += 1;
490  } else if ("endmsg".equals(word)) {
491  state += 2;
492  } else if ("end".equals(word)) {
493  state += 3;
494  } else if ("msg".equals(word)) {
495  state += 4;
496  } else if ("x".equals(word)) {
497  state += 5;
498  } else if ("y".equals(word)) {
499  state += 6;
500  } else if ("type".equals(word)) {
501  state += 7;
502  } else if ("direction".equals(word)) {
503  state += 8;
504  } else if ("face".equals(word)) {
505  state += 9;
506  }
507  }
508  } finally {
509  in.close();
510  }
511  sum += state;
512  }*/
513 
514  /*@PerfTest("DataInputStream, startsWith (current)")
515  public static void currentDI() throws IOException {
516  final DataInputStream in = new DataInputStream(new FileInputStream(testFileName));
517  int state = 0;
518  try {
519  for (String line; (line = in.readLine()) != null;) {
520  line = line.trim();
521  if (line.startsWith("arch ")) {
522  state += 1;
523  } else if (line.startsWith("endmsg")) {
524  state += 2;
525  } else if (line.startsWith("end")) {
526  state += 3;
527  } else if (line.startsWith("msg")) {
528  state += 4;
529  } else if (line.startsWith("x ")) {
530  state += 5;
531  } else if (line.startsWith("y ")) {
532  state += 6;
533  } else if (line.startsWith("type ")) {
534  state += 7;
535  } else if (line.startsWith("direction ")) {
536  state += 8;
537  } else if (line.startsWith("face ")) {
538  state += 9;
539  }
540  }
541  } finally {
542  in.close();
543  }
544  sum += state;
545  }*/
546 
547  /*@PerfTest("DataInputStream, word by regex split of java.lang.String")
548  public static void splitEqualsStringDI() throws IOException {
549  final DataInputStream in = new DataInputStream(new FileInputStream(testFileName));
550  int state = 0;
551  try {
552  for (String line; (line = in.readLine()) != null;) {
553  line = line.trim();
554  final String word = line.split("\\s+")[0];
555  if ("arch".equals(word)) {
556  state += 1;
557  } else if ("endmsg".equals(word)) {
558  state += 2;
559  } else if ("end".equals(word)) {
560  state += 3;
561  } else if ("msg".equals(word)) {
562  state += 4;
563  } else if ("x".equals(word)) {
564  state += 5;
565  } else if ("y".equals(word)) {
566  state += 6;
567  } else if ("type".equals(word)) {
568  state += 7;
569  } else if ("direction".equals(word)) {
570  state += 8;
571  } else if ("face".equals(word)) {
572  state += 9;
573  }
574  }
575  } finally {
576  in.close();
577  }
578  sum += state;
579  }*/
580 
581  /*@PerfTest("DataInputStream, word by regex split of java.util.regex.Pattern")
582  public static void splitEqualsPatternDI() throws IOException {
583  final DataInputStream in = new DataInputStream(new FileInputStream(testFileName));
584  int state = 0;
585  try {
586  for (String line; (line = in.readLine()) != null;) {
587  line = line.trim();
588  final String word = wordSplit.split(line)[0];
589  if ("arch".equals(word)) {
590  state += 1;
591  } else if ("endmsg".equals(word)) {
592  state += 2;
593  } else if ("end".equals(word)) {
594  state += 3;
595  } else if ("msg".equals(word)) {
596  state += 4;
597  } else if ("x".equals(word)) {
598  state += 5;
599  } else if ("y".equals(word)) {
600  state += 6;
601  } else if ("type".equals(word)) {
602  state += 7;
603  } else if ("direction".equals(word)) {
604  state += 8;
605  } else if ("face".equals(word)) {
606  state += 9;
607  }
608  }
609  } finally {
610  in.close();
611  }
612  sum += state;
613  }*/
614 
615  /*@PerfTest("DataInputStream, manual word split using String.indexOf(' ')")
616  public static void manualEqualsDI() throws IOException {
617  final DataInputStream in = new DataInputStream(new FileInputStream(testFileName));
618  int state = 0;
619  try {
620  for (String line; (line = in.readLine()) != null;) {
621  line = line.trim();
622  final int indexOfSpace = line.indexOf(' ');
623  final String word = indexOfSpace > 0 ? line.substring(0, indexOfSpace) : line;
624  if ("arch".equals(word)) {
625  state += 1;
626  } else if ("endmsg".equals(word)) {
627  state += 2;
628  } else if ("end".equals(word)) {
629  state += 3;
630  } else if ("msg".equals(word)) {
631  state += 4;
632  } else if ("x".equals(word)) {
633  state += 5;
634  } else if ("y".equals(word)) {
635  state += 6;
636  } else if ("type".equals(word)) {
637  state += 7;
638  } else if ("direction".equals(word)) {
639  state += 8;
640  } else if ("face".equals(word)) {
641  state += 9;
642  }
643  }
644  } finally {
645  in.close();
646  }
647  sum += state;
648  }*/
649 
650  /*@PerfTest("Buffered DataInputStream, startsWith (current)")
651  public static void currentBDI() throws IOException {
652  final DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(testFileName)));
653  int state = 0;
654  try {
655  for (String line; (line = in.readLine()) != null;) {
656  line = line.trim();
657  if (line.startsWith("arch ")) {
658  state += 1;
659  } else if (line.startsWith("endmsg")) {
660  state += 2;
661  } else if (line.startsWith("end")) {
662  state += 3;
663  } else if (line.startsWith("msg")) {
664  state += 4;
665  } else if (line.startsWith("x ")) {
666  state += 5;
667  } else if (line.startsWith("y ")) {
668  state += 6;
669  } else if (line.startsWith("type ")) {
670  state += 7;
671  } else if (line.startsWith("direction ")) {
672  state += 8;
673  } else if (line.startsWith("face ")) {
674  state += 9;
675  }
676  }
677  } finally {
678  in.close();
679  }
680  sum += state;
681  }*/
682 
683  /*@PerfTest("Buffered DataInputStream, word by regex split of java.lang.String")
684  public static void splitEqualsStringBDI() throws IOException {
685  final DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(testFileName)));
686  int state = 0;
687  try {
688  for (String line; (line = in.readLine()) != null;) {
689  line = line.trim();
690  final String word = line.split("\\s+")[0];
691  if ("arch".equals(word)) {
692  state += 1;
693  } else if ("endmsg".equals(word)) {
694  state += 2;
695  } else if ("end".equals(word)) {
696  state += 3;
697  } else if ("msg".equals(word)) {
698  state += 4;
699  } else if ("x".equals(word)) {
700  state += 5;
701  } else if ("y".equals(word)) {
702  state += 6;
703  } else if ("type".equals(word)) {
704  state += 7;
705  } else if ("direction".equals(word)) {
706  state += 8;
707  } else if ("face".equals(word)) {
708  state += 9;
709  }
710  }
711  } finally {
712  in.close();
713  }
714  sum += state;
715  }*/
716 
717  /*@PerfTest("Buffered DataInputStream, word by regex split of java.util.regex.Pattern")
718  public static void splitEqualsPatternBDI() throws IOException {
719  final DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(testFileName)));
720  int state = 0;
721  try {
722  for (String line; (line = in.readLine()) != null;) {
723  line = line.trim();
724  final String word = wordSplit.split(line)[0];
725  if ("arch".equals(word)) {
726  state += 1;
727  } else if ("endmsg".equals(word)) {
728  state += 2;
729  } else if ("end".equals(word)) {
730  state += 3;
731  } else if ("msg".equals(word)) {
732  state += 4;
733  } else if ("x".equals(word)) {
734  state += 5;
735  } else if ("y".equals(word)) {
736  state += 6;
737  } else if ("type".equals(word)) {
738  state += 7;
739  } else if ("direction".equals(word)) {
740  state += 8;
741  } else if ("face".equals(word)) {
742  state += 9;
743  }
744  }
745  } finally {
746  in.close();
747  }
748  sum += state;
749  }*/
750 
751  /*@PerfTest("Buffered DataInputStream, manual word split using String.indexOf(' ')")
752  public static void manualEqualsBDI() throws IOException {
753  final DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(testFileName)));
754  int state = 0;
755  try {
756  for (String line; (line = in.readLine()) != null;) {
757  line = line.trim();
758  final int indexOfSpace = line.indexOf(' ');
759  final String word = indexOfSpace > 0 ? line.substring(0, indexOfSpace) : line;
760  if ("arch".equals(word)) {
761  state += 1;
762  } else if ("endmsg".equals(word)) {
763  state += 2;
764  } else if ("end".equals(word)) {
765  state += 3;
766  } else if ("msg".equals(word)) {
767  state += 4;
768  } else if ("x".equals(word)) {
769  state += 5;
770  } else if ("y".equals(word)) {
771  state += 6;
772  } else if ("type".equals(word)) {
773  state += 7;
774  } else if ("direction".equals(word)) {
775  state += 8;
776  } else if ("face".equals(word)) {
777  state += 9;
778  }
779  }
780  } finally {
781  in.close();
782  }
783  sum += state;
784  }*/
785 
786  @PerfTest("New File I/O nondirect -> String, manual split of java.lang.String")
787  public static void nioManual() throws IOException {
788  int state = 0;
789  final FileChannel fileChannel = new FileInputStream(testFileName).getChannel();
790  final ByteBuffer buffer = ByteBuffer.allocate((int) fileChannel.size());
791  fileChannel.read(buffer);
792  try {
793  final String file = new String(buffer.array(), 0);
794  for (String line : lineSplit.split(file)) {
795  line = line.trim();
796  final int indexOfSpace = line.indexOf(' ');
797  final String word = indexOfSpace > 0 ? line.substring(0, indexOfSpace) : line;
798  if ("arch".equals(word)) {
799  state += 1;
800  } else if ("endmsg".equals(word)) {
801  state += 2;
802  } else if ("end".equals(word)) {
803  state += 3;
804  } else if ("msg".equals(word)) {
805  state += 4;
806  } else if ("x".equals(word)) {
807  state += 5;
808  } else if ("y".equals(word)) {
809  state += 6;
810  } else if ("type".equals(word)) {
811  state += 7;
812  } else if ("direction".equals(word)) {
813  state += 8;
814  } else if ("face".equals(word)) {
815  state += 9;
816  }
817  }
818  } finally {
819  fileChannel.close();
820  }
821  sum += state;
822  }
823 
824  private static final CharsetDecoder decoder = Charset.forName("iso-8859-1").newDecoder();
825 
826  @PerfTest("Mapped File I/O nondirect -> charset, pattern split")
827  public static void nioChar() throws IOException {
828  int state = 0;
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);
832  try {
833  for (String line : lineSplit.split(charBuffer)) {
834  line = line.trim();
835  final int indexOfSpace = line.indexOf(' ');
836  final String word = indexOfSpace > 0 ? line.substring(0, indexOfSpace) : line;
837  if ("arch".equals(word)) {
838  state += 1;
839  } else if ("endmsg".equals(word)) {
840  state += 2;
841  } else if ("end".equals(word)) {
842  state += 3;
843  } else if ("msg".equals(word)) {
844  state += 4;
845  } else if ("x".equals(word)) {
846  state += 5;
847  } else if ("y".equals(word)) {
848  state += 6;
849  } else if ("type".equals(word)) {
850  state += 7;
851  } else if ("direction".equals(word)) {
852  state += 8;
853  } else if ("face".equals(word)) {
854  state += 9;
855  }
856  }
857  } finally {
858  fileChannel.close();
859  }
860  sum += state;
861  }
862 
863  @PerfTest("BufferdReader -> StringBuilder, manual split of java.lang.String")
864  public static void bufReadManBuf() throws IOException {
865  int state = 0;
866  final BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(testFileName), "iso-8859-1"), 8192);
867  try {
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);
872  }
873  for (String line : lineSplit.split(file)) {
874  line = line.trim();
875  final int indexOfSpace = line.indexOf(' ');
876  final String word = indexOfSpace > 0 ? line.substring(0, indexOfSpace) : line;
877  if ("arch".equals(word)) {
878  state += 1;
879  } else if ("endmsg".equals(word)) {
880  state += 2;
881  } else if ("end".equals(word)) {
882  state += 3;
883  } else if ("msg".equals(word)) {
884  state += 4;
885  } else if ("x".equals(word)) {
886  state += 5;
887  } else if ("y".equals(word)) {
888  state += 6;
889  } else if ("type".equals(word)) {
890  state += 7;
891  } else if ("direction".equals(word)) {
892  state += 8;
893  } else if ("face".equals(word)) {
894  state += 9;
895  }
896  }
897  } finally {
898  in.close();
899  }
900  sum += state;
901  }
902 
903  @PerfTest("BufferedInputStream -> String, manual split")
904  public static void bufReadAll() throws IOException {
905  int state = 0;
906  final int size = (int) new File(testFileName).length();
907  final byte[] buf = new byte[size];
908  final BufferedInputStream in = new BufferedInputStream(new FileInputStream(testFileName));
909  try {
910  for (int pos = 0, bytesRead; pos < size && (bytesRead = in.read(buf, pos, size - pos)) != -1; pos += bytesRead) {
911  }
912  final String file = new String(buf);
913  final StringTokenizer tk = new StringTokenizer(file, "\n");
914  while (tk.hasMoreTokens()) {
915  String line = tk.nextToken();
916  line = line.trim();
917  final int indexOfSpace = line.indexOf(' ');
918  final String word = indexOfSpace > 0 ? line.substring(0, indexOfSpace) : line;
919  if ("arch".equals(word)) {
920  state += 1;
921  } else if ("endmsg".equals(word)) {
922  state += 2;
923  } else if ("end".equals(word)) {
924  state += 3;
925  } else if ("msg".equals(word)) {
926  state += 4;
927  } else if ("x".equals(word)) {
928  state += 5;
929  } else if ("y".equals(word)) {
930  state += 6;
931  } else if ("type".equals(word)) {
932  state += 7;
933  } else if ("direction".equals(word)) {
934  state += 8;
935  } else if ("face".equals(word)) {
936  state += 9;
937  }
938  }
939  } finally {
940  in.close();
941  }
942  sum += state;
943  }
944 
945  @PerfTest("Mapped File I/O nondirect -> String, StringTokenizer")
946  public static void nioByte() throws IOException {
947  int state = 0;
948  final FileChannel fileChannel = new FileInputStream(testFileName).getChannel();
949  final ByteBuffer buffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size());
950  try {
951  final byte[] buf = new byte[buffer.remaining()];
952  buffer.get(buf);
953  final String file = new String(buf);
954  final StringTokenizer tk = new StringTokenizer(file, "\n");
955  while (tk.hasMoreTokens()) {
956  String line = tk.nextToken();
957  line = line.trim();
958  final int indexOfSpace = line.indexOf(' ');
959  final String word = indexOfSpace > 0 ? line.substring(0, indexOfSpace) : line;
960  if ("arch".equals(word)) {
961  state += 1;
962  } else if ("endmsg".equals(word)) {
963  state += 2;
964  } else if ("end".equals(word)) {
965  state += 3;
966  } else if ("msg".equals(word)) {
967  state += 4;
968  } else if ("x".equals(word)) {
969  state += 5;
970  } else if ("y".equals(word)) {
971  state += 6;
972  } else if ("type".equals(word)) {
973  state += 7;
974  } else if ("direction".equals(word)) {
975  state += 8;
976  } else if ("face".equals(word)) {
977  state += 9;
978  }
979  }
980  } finally {
981  fileChannel.close();
982  }
983  sum += state;
984  }
985 
986 }
PerfTestMapDecode.bufReadManBuf
static void bufReadManBuf()
Definition: PerfTestMapDecode.java:864
LongValueComparator.getInstance
static Comparator< Map.Entry< Method, Long > > getInstance()
Definition: PerfTestMapDecode.java:13
PerfTestMapDecode.hashArch
static final int hashArch
Definition: PerfTestMapDecode.java:270
PerfTestMapDecode.hashY
static final int hashY
Definition: PerfTestMapDecode.java:280
PerfTestMapDecode.testFileName
static String testFileName
Definition: PerfTestMapDecode.java:23
PerfTestMapDecode.main
static void main(final String... args)
Definition: PerfTestMapDecode.java:31
LongValueComparator.LongValueComparator
LongValueComparator()
Definition: PerfTestMapDecode.java:8
PerfTestMapDecode.splitEqualsString
static void splitEqualsString()
Definition: PerfTestMapDecode.java:132
PerfTestMapDecode.hashFace
static final int hashFace
Definition: PerfTestMapDecode.java:286
PerfTestMapDecode.hashes
static final int[] hashes
Definition: PerfTestMapDecode.java:288
LongValueComparator.compare
int compare(final Map.Entry< Method, Long > o1, final Map.Entry< Method, Long > o2)
Definition: PerfTestMapDecode.java:3
PerfTestMapDecode.hashEnd
static final int hashEnd
Definition: PerfTestMapDecode.java:274
PerfTestMapDecode.hashMsg
static final int hashMsg
Definition: PerfTestMapDecode.java:276
PerfTestMapDecode.manualEqualsCashedHashSwitch
static void manualEqualsCashedHashSwitch()
Definition: PerfTestMapDecode.java:327
PerfTestMapDecode.NUM_OF_TEST_REPETITIONS
static final int NUM_OF_TEST_REPETITIONS
Definition: PerfTestMapDecode.java:21
PerfTestMapDecode.wordSplit
static Pattern wordSplit
Definition: PerfTestMapDecode.java:29
PerfTestMapDecode.manualEqualsCashedHash
static void manualEqualsCashedHash()
Definition: PerfTestMapDecode.java:291
LongValueComparator.instance
static LongValueComparator instance
Definition: PerfTestMapDecode.java:11
PerfTestMapDecode.nioManual
static void nioManual()
Definition: PerfTestMapDecode.java:787
PerfTestMapDecode.hashEndmsg
static final int hashEndmsg
Definition: PerfTestMapDecode.java:272
PerfTestMapDecode.nioChar
static void nioChar()
Definition: PerfTestMapDecode.java:827
PerfTestMapDecode.nioByte
static void nioByte()
Definition: PerfTestMapDecode.java:946
PerfTestMapDecode.hashX
static final int hashX
Definition: PerfTestMapDecode.java:278
PerfTestMapDecode.lineSplit
static Pattern lineSplit
Definition: PerfTestMapDecode.java:27
PerfTestMapDecode.splitEqualsPattern
static void splitEqualsPattern()
Definition: PerfTestMapDecode.java:166
LongValueComparator
Definition: PerfTestMapDecode.java:1
PerfTestMapDecode.decoder
static final CharsetDecoder decoder
Definition: PerfTestMapDecode.java:824
PerfTestMapDecode.manualEquals
static void manualEquals()
Definition: PerfTestMapDecode.java:200
PerfTestMapDecode.bufReadAll
static void bufReadAll()
Definition: PerfTestMapDecode.java:904
PerfTestMapDecode.hashType
static final int hashType
Definition: PerfTestMapDecode.java:282
PerfTestMapDecode.manualEqualsHash
static void manualEqualsHash()
Definition: PerfTestMapDecode.java:235
PerfTest
Definition: PerfTest.java:8
PerfTestMapDecode.current
static void current()
Definition: PerfTestMapDecode.java:99
PerfTestMapDecode.old
static void old()
Definition: PerfTestMapDecode.java:66
PerfTestMapDecode.sum
static int sum
Definition: PerfTestMapDecode.java:25
PerfTestMapDecode
Definition: PerfTestMapDecode.java:19
PerfTestMapDecode.hashDirection
static final int hashDirection
Definition: PerfTestMapDecode.java:284