src.it.unimi.dsi.test.TextPatternSpeedTest Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dsiutils Show documentation
Show all versions of dsiutils Show documentation
The DSI utilities are a mishmash of classes accumulated during the last twenty years in projects developed at the DSI (Dipartimento di Scienze dell'Informazione, i.e., Information Sciences Department), now DI (Dipartimento di Informatica, i.e., Informatics Department), of the Universita` degli Studi di Milano.
package it.unimi.dsi.test;
import it.unimi.dsi.Util;
import it.unimi.dsi.lang.MutableString;
import it.unimi.dsi.util.TextPattern;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class TextPatternSpeedTest {
private TextPatternSpeedTest() {}
public static void main(final String[] arg) {
String target = null;
MutableString ms = new MutableString();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line;
while ((line = br.readLine()) != null) ms.append("\n").append(line);
ms.compact();
target = ms.toString();
} catch (IOException e) {
System.out.println("Problems while reading target");
e.printStackTrace(System.out);
System.exit(1);
}
int i, n;
String p = arg[0];
int k;
long start;
System.out.println("Searching for " + arg[0]);
for(k = 10; k-- != 0;) {
System.out.println();
start = - System.nanoTime();
n = 0;
for(int r = 100; r-- != 0;) {
i = -1;
do {
i = target.indexOf(p, i + 1);
n++;
} while(i != -1);
}
start += System.nanoTime();
System.out.println("Called indexOf() " + n + " times on a string in " + start + " ns (" + Util.format(start / (double)n) + " ns/call)");
TextPattern tp = new TextPattern(p);
char a[] = ms.array();
start = - System.nanoTime();
n = 0;
for(int r = 100; r-- != 0;) {
i = -1;
do {
i = tp.search(a, i + 1);
n++;
} while(i != -1);
}
start += System.nanoTime();
System.out.println("Called search() " + n + " times on a string in " + start + " ns (" + Util.format(start / (double)n) + " ns/call)");
MutableString pattern = new MutableString(p);
start = - System.nanoTime();
n = 0;
for(int r = 100; r-- != 0;) {
i = -1;
do {
i = ms.indexOf(pattern, i + 1);
n++;
} while(i != -1);
}
start += System.nanoTime();
System.out.println("Called indexOf() " + n + " times on a mutable string in " + start + " ns (" + Util.format(start / (double)n) + " ns/call)");
}
}
}