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