diff --git a/BenchmarkRunner.java b/BenchmarkRunner.java new file mode 100644 index 0000000..1bb0a1a --- /dev/null +++ b/BenchmarkRunner.java @@ -0,0 +1,73 @@ +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.lang.reflect.Method; +import java.util.*; + +public class BenchmarkRunner { + + private static final int WARMUP_RUNS = 2; + private static final int MEASURED_RUNS = 5; + + static List load() throws IOException { + ArrayList lines = new ArrayList(1000000); + + try (BufferedReader br = new BufferedReader(new FileReader("test_data.out"))) { + String line = br.readLine(); + while (line != null) { + while (!line.endsWith("\"")) { + String nextLine = br.readLine(); + if (nextLine == null) { + break; + } + line = line + nextLine; + } + if (line.startsWith("\"")) { + line = line.substring(1); + } + if (line.endsWith("\"")) { + line = line.substring(0, line.length() - 1); + } + lines.add(line); + line = br.readLine(); + } + } + + return lines; + } + + public static void main(String[] args) throws Exception { + List classNames = Arrays.asList( + "SearchByContains", + "SearchByManualLoop", + "SearchByRegex", + "SearchByArray" + ); + + List data = load(); + + for (String className : classNames) { + System.out.println("Benchmarking " + className + "..."); + + Class clazz = Class.forName(className); + Method mainMethod = clazz.getMethod("count", List.class); + Integer count = 0; + + for (int i = 0; i < WARMUP_RUNS; i++) { + count = (Integer) mainMethod.invoke(null, data); + } + + long totalTime = 0; + for (int i = 0; i < MEASURED_RUNS; i++) { + long start = System.nanoTime(); + mainMethod.invoke(null, data); + long end = System.nanoTime(); + totalTime += (end - start); + } + + double avgMs = totalTime / MEASURED_RUNS / 1_000_000.0; + System.out.printf("Average time: %.3f ms%nCount: %d%n", avgMs, count); + System.out.println(); + } + } +} diff --git a/MakeTestData.java b/MakeTestData.java index a65c1d0..6c72ad8 100644 --- a/MakeTestData.java +++ b/MakeTestData.java @@ -19,6 +19,8 @@ public class MakeTestData { public static void main(String [] args) throws Exception { Random random = new Random(); + random.setSeed(0); // Reproducibility + StringBuilder sb = new StringBuilder(); try(FileWriter fw = new FileWriter("test_data.out")) { for (int i = 0; i < NUM_WORDS; i++) { diff --git a/SearchByArray.java b/SearchByArray.java new file mode 100644 index 0000000..f90c19c --- /dev/null +++ b/SearchByArray.java @@ -0,0 +1,24 @@ +import java.util.*; + +public class SearchByArray { + public static int count(List data) { + int count = 0; + + // Lookup tables my beloved + boolean[] match = new boolean[256]; + match[','] = true; + match['\n'] = true; + match['\r'] = true; + match['\"'] = true; + + for (String line : data) { + for (char c : line.toCharArray()) { + if (c < 256 && match[c]) { + count++; + break; + } + } + } + return count; + } +} diff --git a/SearchByContains.java b/SearchByContains.java index 6b0e2a0..d8574d1 100644 --- a/SearchByContains.java +++ b/SearchByContains.java @@ -1,34 +1,14 @@ -import java.io.*; +import java.util.*; public class SearchByContains { - - public static void main(String [] args) throws Exception { + public static int count(List data) { int count = 0; - try (BufferedReader br = new BufferedReader(new FileReader("test_data.out"))) { - String line = br.readLine(); - while (line != null) { - while (!line.endsWith("\"")) { - String nextLine = br.readLine(); - if (nextLine == null) { - break; - } - line = line + nextLine; - } - if (line.startsWith("\"")) { - line = line.substring(1); - } - if (line.endsWith("\"")) { - line = line.substring(0, line.length() - 1); - } - if (line.contains("\"") || line.contains(",") || line.contains("\r") || + for (String line : data) { + if (line.contains("\"") || line.contains(",") || line.contains("\r") || line.contains("\n")) { - count++; - } - line = br.readLine(); + count++; } } - System.out.println("Search By Four 'contains' calls."); - System.out.println("Counted " + count + " lines that would have needed to be quoted."); + return count; } - } diff --git a/SearchByManualLoop.java b/SearchByManualLoop.java index 0970a85..8dbc2f8 100644 --- a/SearchByManualLoop.java +++ b/SearchByManualLoop.java @@ -1,43 +1,22 @@ -import java.io.*; import java.util.*; public class SearchByManualLoop { - static final int cpQuote = "\"".codePointAt(0); - static final int cpComma = ",".codePointAt(0); - static final int cpCr = "\r".codePointAt(0); - static final int cpNl = "\n".codePointAt(0); + static final char cpQuote = '\"'; + static final char cpComma = ','; + static final char cpCr = '\r'; + static final char cpNl = '\n'; - public static void main(String [] args) throws Exception { + public static int count(List data) { int count = 0; - try (BufferedReader br = new BufferedReader(new FileReader("test_data.out"))) { - String line = br.readLine(); - while (line != null) { - while (!line.endsWith("\"")) { - String nextLine = br.readLine(); - if (nextLine == null) { - break; - } - line = line + nextLine; + for (String line : data) { + for (char c : line.toCharArray()) { + if (c == cpQuote || c == cpComma || c == cpCr || c == cpNl) { + count++; + break; } - if (line.startsWith("\"")) { - line = line.substring(1); - } - if (line.endsWith("\"")) { - line = line.substring(0, line.length() - 1); - } - for (int i = 0; i < line.length(); i++) { - int c = line.codePointAt(i); - if (c == cpQuote || c == cpComma || c == cpCr || c == cpNl) { - count++; - break; - } - } - line = br.readLine(); } } - System.out.println("Search By Manual Loop:"); - System.out.println("Counted " + count + " lines that would have needed to be quoted."); + return count; } - } diff --git a/SearchByRegex.java b/SearchByRegex.java index dbc4bfe..648b548 100644 --- a/SearchByRegex.java +++ b/SearchByRegex.java @@ -1,33 +1,16 @@ -import java.io.*; +import java.util.List; +import java.util.regex.*; public class SearchByRegex { - - public static void main(String [] args) throws Exception { + public static int count(List data) { + Pattern regex = Pattern.compile("[\",\\r\\n]"); int count = 0; - try (BufferedReader br = new BufferedReader(new FileReader("test_data.out"))) { - String line = br.readLine(); - while (line != null) { - while (!line.endsWith("\"")) { - String nextLine = br.readLine(); - if (nextLine == null) { - break; - } - line = line + nextLine; - } - if (line.startsWith("\"")) { - line = line.substring(1); - } - if (line.endsWith("\"")) { - line = line.substring(0, line.length() - 1); - } - if (line.matches("^[^\",\r\n]*[\",\r\n].*")) { - count++; - } - line = br.readLine(); + for (String line : data) { + Matcher matcher = regex.matcher(line); + if (matcher.find()) { + count++; } } - System.out.println("Search by Regex:"); - System.out.println("Counted " + count + " lines that would have needed to be quoted."); + return count; } - } diff --git a/run_benchmark.sh b/run_benchmark.sh index f68221a..68f3182 100755 --- a/run_benchmark.sh +++ b/run_benchmark.sh @@ -3,26 +3,17 @@ main() { cat <