Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions BenchmarkRunner.java
Original file line number Diff line number Diff line change
@@ -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<String> load() throws IOException {
ArrayList<String> lines = new ArrayList<String>(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<String> classNames = Arrays.asList(
"SearchByContains",
"SearchByManualLoop",
"SearchByRegex",
"SearchByArray"
);

List<String> 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();
}
}
}
2 changes: 2 additions & 0 deletions MakeTestData.java
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand Down
24 changes: 24 additions & 0 deletions SearchByArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import java.util.*;

public class SearchByArray {
public static int count(List<String> 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;
}
}
32 changes: 6 additions & 26 deletions SearchByContains.java
Original file line number Diff line number Diff line change
@@ -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<String> 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;
}

}
43 changes: 11 additions & 32 deletions SearchByManualLoop.java
Original file line number Diff line number Diff line change
@@ -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<String> 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;
}

}
35 changes: 9 additions & 26 deletions SearchByRegex.java
Original file line number Diff line number Diff line change
@@ -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<String> 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;
}

}
25 changes: 8 additions & 17 deletions run_benchmark.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,17 @@

main() {
cat <<EOF
This runs a simple benchmark.
You need /usr/bin/time (for more granular data
than plain 'time'), javac, and java.
This runs a simple benchmark.
You need javac and java.
EOF

javac MakeTestData.java
echo " Compiling..."
javac *.java
echo " Generating Data..."
java MakeTestData
javac SearchByContains.java
javac SearchByManualLoop.java
javac SearchByRegex.java
echo running each benchmark three times
/usr/bin/time java SearchByManualLoop
/usr/bin/time java SearchByManualLoop
/usr/bin/time java SearchByManualLoop
/usr/bin/time java SearchByContains
/usr/bin/time java SearchByContains
/usr/bin/time java SearchByContains
/usr/bin/time java SearchByRegex
/usr/bin/time java SearchByRegex
/usr/bin/time java SearchByRegex
echo " Starting benchmark!"
echo "---------------------"
java BenchmarkRunner
exit
}

Expand Down