1 class LongValueComparator
implements Comparator<Map.Entry<Method, Long>> {
3 public int compare(
final Map.Entry<Method, Long> o1,
final Map.Entry<Method, Long> o2) {
4 final long d = o1.getValue() - o2.getValue();
8 o1.getKey().getName().compareTo(o2.getKey().getName());
11 private LongValueComparator() {
14 private static LongValueComparator instance =
new LongValueComparator();
16 public static Comparator<Map.Entry<Method, Long>> getInstance() {
24 public static final int NUM_OF_TEST_REPETITIONS = 200;
28 public static int sum;
30 private static Pattern lineSplit = Pattern.compile(
"\n");
32 private static Pattern wordSplit = Pattern.compile(
"\\s+");
34 public static void main(
final String... args)
throws Throwable {
35 testFileName = args[0];
36 final List<Method> performanceTestMethods =
new ArrayList<Method>();
38 if (method.getAnnotation(
PerfTest.class) != null) {
39 performanceTestMethods.add(method);
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) {
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);
53 final long end = System.currentTimeMillis();
54 final long time = end - start;
55 results.put(performanceTestMethod, time);
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());
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"));
73 for (String line; (line = in.readLine()) != null; ) {
75 if (line.regionMatches(0,
"arch ", 0, 5)) {
77 }
else if (line.regionMatches(0,
"endmsg", 0, 6)) {
79 }
else if (line.regionMatches(0,
"end", 0, 3)) {
81 }
else if (line.regionMatches(0,
"msg", 0, 3)) {
83 }
else if (line.regionMatches(0,
"x ", 0, 2)) {
85 }
else if (line.regionMatches(0,
"y ", 0, 2)) {
87 }
else if (line.regionMatches(0,
"type ", 0, 5)) {
89 }
else if (line.regionMatches(0,
"direction ", 0, 10)) {
91 }
else if (line.regionMatches(0,
"face ", 0, 5)) {
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"));
106 for (String line; (line = in.readLine()) != null; ) {
108 if (line.startsWith(
"arch ")) {
110 }
else if (line.startsWith(
"endmsg")) {
112 }
else if (line.startsWith(
"end")) {
114 }
else if (line.startsWith(
"msg")) {
116 }
else if (line.startsWith(
"x ")) {
118 }
else if (line.startsWith(
"y ")) {
120 }
else if (line.startsWith(
"type ")) {
122 }
else if (line.startsWith(
"direction ")) {
124 }
else if (line.startsWith(
"face ")) {
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"));
139 for (String line; (line = in.readLine()) != null; ) {
141 final String word = line.split(
"\\s+")[0];
142 if (
"arch".equals(word)) {
144 }
else if (
"endmsg".equals(word)) {
146 }
else if (
"end".equals(word)) {
148 }
else if (
"msg".equals(word)) {
150 }
else if (
"x".equals(word)) {
152 }
else if (
"y".equals(word)) {
154 }
else if (
"type".equals(word)) {
156 }
else if (
"direction".equals(word)) {
158 }
else if (
"face".equals(word)) {
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"));
173 for (String line; (line = in.readLine()) != null; ) {
175 final String word = wordSplit.split(line)[0];
176 if (
"arch".equals(word)) {
178 }
else if (
"endmsg".equals(word)) {
180 }
else if (
"end".equals(word)) {
182 }
else if (
"msg".equals(word)) {
184 }
else if (
"x".equals(word)) {
186 }
else if (
"y".equals(word)) {
188 }
else if (
"type".equals(word)) {
190 }
else if (
"direction".equals(word)) {
192 }
else if (
"face".equals(word)) {
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"));
207 for (String line; (line = in.readLine()) != null; ) {
209 final int indexOfSpace = line.indexOf(
' ');
210 final String word = indexOfSpace > 0 ? line.substring(0, indexOfSpace) : line;
211 if (
"arch".equals(word)) {
213 }
else if (
"endmsg".equals(word)) {
215 }
else if (
"end".equals(word)) {
217 }
else if (
"msg".equals(word)) {
219 }
else if (
"x".equals(word)) {
221 }
else if (
"y".equals(word)) {
223 }
else if (
"type".equals(word)) {
225 }
else if (
"direction".equals(word)) {
227 }
else if (
"face".equals(word)) {
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"));
242 for (String line; (line = in.readLine()) != null; ) {
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) {
249 }
else if (
"endmsg".hashCode() == hash) {
251 }
else if (
"end".hashCode() == hash) {
253 }
else if (
"msg".hashCode() == hash) {
255 }
else if (
"x".hashCode() == hash) {
257 }
else if (
"y".hashCode() == hash) {
259 }
else if (
"type".hashCode() == hash) {
261 }
else if (
"direction".hashCode() == hash) {
263 }
else if (
"face".hashCode() == hash) {
273 private static final int hashArch =
"arch".hashCode();
275 private static final int hashEndmsg =
"endmsg".hashCode();
277 private static final int hashEnd =
"end".hashCode();
279 private static final int hashMsg =
"msg".hashCode();
281 private static final int hashX =
"x".hashCode();
283 private static final int hashY =
"y".hashCode();
285 private static final int hashType =
"type".hashCode();
287 private static final int hashDirection =
"direction".hashCode();
289 private static final int hashFace =
"face".hashCode();
291 private static final int[] hashes = {
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"));
308 for (String line; (line = in.readLine()) != null; ) {
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) {
315 }
else if (hash == hashEndmsg) {
317 }
else if (hash == hashEnd) {
319 }
else if (hash == hashMsg) {
321 }
else if (hash == hashX) {
323 }
else if (hash == hashY) {
325 }
else if (hash == hashType) {
327 }
else if (hash == hashDirection) {
329 }
else if (hash == hashFace) {
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"));
344 for (String line; (line = in.readLine()) != null; ) {
346 final int indexOfSpace = line.indexOf(
' ');
347 final String word = indexOfSpace > 0 ? line.substring(0, indexOfSpace) : line;
348 final int hash = word.hashCode();
350 for (hashIndex = 0; hashIndex < hashes.length; hashIndex++) {
351 if (hashes[hashIndex] == hash) {
799 @
PerfTest(
"New File I/O nondirect -> String, manual split of java.lang.String")
800 public static
void nioManual() throws IOException {
802 final FileChannel fileChannel =
new FileInputStream(testFileName).getChannel();
803 final ByteBuffer buffer = ByteBuffer.allocate((
int) fileChannel.size());
804 fileChannel.read(buffer);
806 final String file =
new String(buffer.array(), 0);
807 for (String line : lineSplit.split(file)) {
809 final int indexOfSpace = line.indexOf(
' ');
810 final String word = indexOfSpace > 0 ? line.substring(0, indexOfSpace) : line;
811 if (
"arch".equals(word)) {
813 }
else if (
"endmsg".equals(word)) {
815 }
else if (
"end".equals(word)) {
817 }
else if (
"msg".equals(word)) {
819 }
else if (
"x".equals(word)) {
821 }
else if (
"y".equals(word)) {
823 }
else if (
"type".equals(word)) {
825 }
else if (
"direction".equals(word)) {
827 }
else if (
"face".equals(word)) {
837 private static final CharsetDecoder decoder = Charset.forName(
"iso-8859-1").newDecoder();
839 @
PerfTest(
"Mapped File I/O nondirect -> charset, pattern split")
840 public static
void nioChar() throws IOException {
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);
846 for (String line : lineSplit.split(charBuffer)) {
848 final int indexOfSpace = line.indexOf(
' ');
849 final String word = indexOfSpace > 0 ? line.substring(0, indexOfSpace) : line;
850 if (
"arch".equals(word)) {
852 }
else if (
"endmsg".equals(word)) {
854 }
else if (
"end".equals(word)) {
856 }
else if (
"msg".equals(word)) {
858 }
else if (
"x".equals(word)) {
860 }
else if (
"y".equals(word)) {
862 }
else if (
"type".equals(word)) {
864 }
else if (
"direction".equals(word)) {
866 }
else if (
"face".equals(word)) {
876 @
PerfTest(
"BufferdReader -> StringBuilder, manual split of java.lang.String")
877 public static
void bufReadManBuf() throws IOException {
879 final BufferedReader in =
new BufferedReader(
new InputStreamReader(
new FileInputStream(testFileName),
"iso-8859-1"), 8192);
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);
886 for (String line : lineSplit.split(file)) {
888 final int indexOfSpace = line.indexOf(
' ');
889 final String word = indexOfSpace > 0 ? line.substring(0, indexOfSpace) : line;
890 if (
"arch".equals(word)) {
892 }
else if (
"endmsg".equals(word)) {
894 }
else if (
"end".equals(word)) {
896 }
else if (
"msg".equals(word)) {
898 }
else if (
"x".equals(word)) {
900 }
else if (
"y".equals(word)) {
902 }
else if (
"type".equals(word)) {
904 }
else if (
"direction".equals(word)) {
906 }
else if (
"face".equals(word)) {
916 @
PerfTest(
"BufferedInputStream -> String, manual split")
917 public static
void bufReadAll() throws IOException {
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));
923 for (
int pos = 0, bytesRead; pos < size && (bytesRead = in.read(buf, pos, size - pos)) != -1; pos += bytesRead)
925 final String file =
new String(buf);
926 final StringTokenizer tk =
new StringTokenizer(file,
"\n");
927 while (tk.hasMoreTokens()) {
928 String line = tk.nextToken();
930 final int indexOfSpace = line.indexOf(
' ');
931 final String word = indexOfSpace > 0 ? line.substring(0, indexOfSpace) : line;
932 if (
"arch".equals(word)) {
934 }
else if (
"endmsg".equals(word)) {
936 }
else if (
"end".equals(word)) {
938 }
else if (
"msg".equals(word)) {
940 }
else if (
"x".equals(word)) {
942 }
else if (
"y".equals(word)) {
944 }
else if (
"type".equals(word)) {
946 }
else if (
"direction".equals(word)) {
948 }
else if (
"face".equals(word)) {
958 @
PerfTest(
"Mapped File I/O nondirect -> String, StringTokenizer")
959 public static
void nioByte() throws IOException {
961 final FileChannel fileChannel =
new FileInputStream(testFileName).getChannel();
962 final ByteBuffer buffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size());
964 final byte[] buf =
new byte[buffer.remaining()];
966 final String file =
new String(buf);
967 final StringTokenizer tk =
new StringTokenizer(file,
"\n");
968 while (tk.hasMoreTokens()) {
969 String line = tk.nextToken();
971 final int indexOfSpace = line.indexOf(
' ');
972 final String word = indexOfSpace > 0 ? line.substring(0, indexOfSpace) : line;
973 if (
"arch".equals(word)) {
975 }
else if (
"endmsg".equals(word)) {
977 }
else if (
"end".equals(word)) {
979 }
else if (
"msg".equals(word)) {
981 }
else if (
"x".equals(word)) {
983 }
else if (
"y".equals(word)) {
985 }
else if (
"type".equals(word)) {
987 }
else if (
"direction".equals(word)) {
989 }
else if (
"face".equals(word)) {
static void main(final String... args)
static String testFileName