-
-
Save hlapp/3946836 to your computer and use it in GitHub Desktop.
Test the performance of java on regex. There are 5 cases. I put the running time of each on my machine as comment. You can remove the "/*" and "*/" of any case to run on it.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.regex.Pattern; | |
import java.util.regex.Matcher; | |
public class benchmarking_java { | |
public static void main(String[] args) { | |
// regular expression | |
//Pattern pattern = Pattern.compile("^.*(.*?)<i>\\s*([^<]*)\\s*<\\/i>(.*).*$"); | |
Pattern pattern = Pattern.compile("^(.*?)<i>\\s*([^<]*)\\s*<\\/i>(.*)$"); | |
long startTime = 0; | |
long endTime = 0; | |
long totalTime = 0; | |
String str1 = "a ligament (e.g. <i>Distichodus</i>, Vari, 1979, fig. 42 )or sheath of connective tissue (e.g. <i>Hoplias</i> )connected to the medial face of the maxilla, and permitting considerable freedom of movement to the posterior end of the maxilla"; | |
String str2 = ", Vari, 1979, fig. 42 )or sheath of connective tissue (e.g. <i>Hoplias</i> )connected to the medial face of the maxilla, and permitting considerable freedom of movement to the posterior end of the maxilla"; | |
String str3 = " )connected to the medial face of the maxilla, and permitting considerable freedom of movement to the posterior end of the maxilla"; | |
String str4 = "ectopterygoid inserted on the ventral surface of the area near the vomer-rnesethmoid articulation (hypothesized fusion in <i>Hepsetus odoe</i> )by means of a ligament attached to a socket or bony facet"; | |
String str5 = " )by means of a ligament attached to a socket or bony facet"; | |
// run the test cases for a given # of iterations | |
for (int j = 1; j <= 100*1000; j = j * 10) { | |
System.out.println("# of Iteration: " + j); | |
// test cases | |
Matcher matcher1 = pattern | |
.matcher(str1); | |
Matcher matcher2 = pattern | |
.matcher(str2); | |
Matcher matcher3 = pattern | |
.matcher(str3); | |
Matcher matcher4 = pattern | |
.matcher(str4); | |
Matcher matcher5 = pattern | |
.matcher(str5); | |
// start timer | |
startTime = System.nanoTime(); | |
for (int i = 0; i < j; i++) { | |
//print out matches (true or false) | |
/* | |
System.out.println(matcher1.find(0)); | |
System.out.println(matcher2.find(0)); | |
System.out.println(matcher3.find(0)); | |
System.out.println(matcher4.find(0)); | |
System.out.println(matcher5.find(0)); | |
//true | |
//true | |
//false | |
//true | |
//false | |
*/ | |
//////////////////////// | |
//case 1 | |
/* | |
if(str1.contains("<")) { | |
matcher1.find(0); | |
} | |
if(str2.contains("<")) { | |
matcher2.find(0); | |
} | |
if(str3.contains("<")) { | |
matcher3.find(0); | |
} | |
if(str4.contains("<")) { | |
matcher4.find(0); | |
} | |
if(str5.contains("<")) { | |
matcher5.find(0); | |
} | |
*/ | |
/*result | |
# of Iteration: 1 | |
Time: 0.001085 seconds | |
# of Iteration: 10 | |
Time: 0.007947 seconds | |
# of Iteration: 100 | |
Time: 0.082454 seconds | |
# of Iteration: 1000 | |
Time: 0.03314 seconds | |
# of Iteration: 10000 | |
Time: 0.189948 seconds | |
# of Iteration: 100000 | |
Time: 1.47002 seconds | |
*/ | |
//////////////////////// | |
//case 2 | |
/* | |
if(str1.contains("<")) { | |
matcher1.matches(); | |
} | |
if(str2.contains("<")) { | |
matcher2.matches(); | |
} | |
if(str3.contains("<")) { | |
matcher3.matches(); | |
} | |
if(str4.contains("<")) { | |
matcher4.matches(); | |
} | |
if(str5.contains("<")) { | |
matcher5.matches(); | |
} | |
*/ | |
/*result | |
# of Iteration: 1 | |
Time: 0.001275 seconds | |
# of Iteration: 10 | |
Time: 0.008449 seconds | |
# of Iteration: 100 | |
Time: 0.098383 seconds | |
# of Iteration: 1000 | |
Time: 0.03417 seconds | |
# of Iteration: 10000 | |
Time: 0.159722 seconds | |
# of Iteration: 100000 | |
Time: 1.533606 seconds | |
*/ | |
//////////////////////// | |
//case 3 | |
/* | |
matcher1.find(0); | |
matcher2.find(0); | |
matcher3.find(0); | |
matcher4.find(0); | |
matcher5.find(0); | |
*/ | |
/*result | |
# of Iteration: 1 | |
Time: 0.001833 seconds | |
# of Iteration: 10 | |
Time: 0.012524 seconds | |
# of Iteration: 100 | |
Time: 0.056948 seconds | |
# of Iteration: 1000 | |
Time: 0.043023 seconds | |
# of Iteration: 10000 | |
Time: 0.236856 seconds | |
# of Iteration: 100000 | |
Time: 2.108658 seconds | |
*/ | |
//////////////////////// | |
//case 4 | |
/* | |
matcher1.matches(); | |
matcher2.matches(); | |
matcher3.matches(); | |
matcher4.matches(); | |
matcher5.matches(); | |
*/ | |
/*result | |
# of Iteration: 1 | |
Time: 0.00624 seconds | |
# of Iteration: 10 | |
Time: 0.027168 seconds | |
# of Iteration: 100 | |
Time: 0.06416 seconds | |
# of Iteration: 1000 | |
Time: 0.083688 seconds | |
# of Iteration: 10000 | |
Time: 0.251729 seconds | |
# of Iteration: 100000 | |
Time: 2.188301 seconds | |
*/ | |
//////////////////////// | |
//case 5 | |
/* | |
str1.matches("^(.*?)<i>\\s*([^<]*)\\s*<\\/i>(.*)$"); | |
str2.matches("^(.*?)<i>\\s*([^<]*)\\s*<\\/i>(.*)$"); | |
str3.matches("^(.*?)<i>\\s*([^<]*)\\s*<\\/i>(.*)$"); | |
str4.matches("^(.*?)<i>\\s*([^<]*)\\s*<\\/i>(.*)$"); | |
str5.matches("^(.*?)<i>\\s*([^<]*)\\s*<\\/i>(.*)$"); | |
*/ | |
/*result | |
# of Iteration: 1 | |
Time: 0.002021 seconds | |
# of Iteration: 10 | |
Time: 0.023815 seconds | |
# of Iteration: 100 | |
Time: 0.160201 seconds | |
# of Iteration: 1000 | |
Time: 0.220326 seconds | |
# of Iteration: 10000 | |
Time: 0.391955 seconds | |
# of Iteration: 100000 | |
Time: 3.002674 seconds | |
*/ | |
} | |
// end timer | |
endTime = System.nanoTime(); | |
// print out time | |
totalTime = endTime - startTime; | |
// System.out.println("Start Time: "+startTime +" seconds\n"); | |
// System.out.println("End Time: "+endTime +" seconds\n"); | |
System.out.println("Time: " + (double) totalTime | |
/ (1000 * 1000 * 1000) + " seconds\n"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment