the efficiency of Java regular ExpressionsAs for the efficiency of regular expressions, I see two different results on the Internet, how efficient it is, and today I did a test with Java. The problem solved is simply to save the string from a string into a list with regular expressions like href= "[^\"]* ". A long string is constructed before the matching operation is performed. The test code is as follows:
Import java.util.LinkedList;
Import java.util.List;
Import Java.util.regex.Matcher;
Import Java.util.regex.Pattern;
public class Reg {
Public String content = "";
Public List userreglist = new LinkedList ();
Public List usercommonlist = new LinkedList ();
Public List useronetoonelist = new LinkedList ();
The passed parameter is the length of the constructed string
public void initcontent (int count) {
Choose StringBuffer, you can choose string a try, reflecting the efficiency difference between the two stitching strings
StringBuffer sb = new StringBuffer ();
for (int i = 0; i < count/20; i++) {
In order to simplify, the unification uses a small section to circulate
Sb.append ("ABC href=\" abcd\ "ABCE");
}
This.content = Sb.tostring ();
}
Using regular expression methods
public void Useregcode () {
Pattern p = pattern.compile ("href=\" [^\ "]*\" ", pattern.canon_eq);
Matcher match = P.matcher (this.content);
while (Match.find ()) {
Userreglist.add (Match.group (0));
}
}
The main use of the Find function provided in the string is to implement
public void Usecommoncode () {
int i = 0;
int index = 0;
while (index = Content.indexof ("href=\", I))!=-1) {
int endindex = Content.indexof ("\" ", index + 6);
i = Endindex + 1;
Usercommonlist.add (content.substring (index, Endindex + 1));
}
}
One character to traverse the judgment
public void Useonetoonecode () {
int length = Content.length ();
for (int i = 0; i < length; i++) {
//The implementation here is somewhat dependent on the header character of the target, in order to simply ignore
if (Content.charat (i) = = ' h ' && i + 5 < length
&& content.substring (i, i + 5). Equals ("href=")) {
Int J = i + 6;
Boolean match = false;
for (; j < length; J +) {
if (content.cha RAt (j) = = ' \ ') {
match = true;
break;
}
}
j + +;
if (match) {
useronetoonelist.add ( Content.substring (i, j));
}
i = j;
}
}
}
Consumption of time records, in order to avoid interference before and after the program, in a specific test, one to test the record time
public static void Main (string[] args) {
Reg r = new Reg ();
R.initcontent (8000000);
Using Onetoone
Long T5 = System.currenttimemillis ();
R.useonetoonecode ();
Long T6 = System.currenttimemillis ();
SYSTEM.OUT.PRINTLN ("Your program is running:" + (int) (T6-T5)/1000 + "seconds"
+ ((T6-T5)% 1000) + "millisecond");
Using regular expressions
long t3 = System.currenttimemillis ();
R.useregcode ();
long T4 = System.currenttimemillis ();
SYSTEM.OUT.PRINTLN ("Your program is running:" + (int) (T4-T3)/1000 + "seconds"
+ ((T4-T3)% 1000) + "millisecond");
Use Find
Long T1 = System.currenttimemillis ();
R.usecommoncode ();
Long t2 = System.currenttimemillis ();
SYSTEM.OUT.PRINTLN ("Your program is running:" + (int) (T2-T1)/1000 + "seconds"
+ ((T2-T1)% 1000) + "millisecond");
}
}
In the test process, each way runs separately, the data quantity is 8000000 characters, the data is as follows, the ordinate unit is the millisecond, the horizontal coordinate unit is the time, like Figure 1.1
Figure 1.1
When the amount of data is small, such as 800 characters to 8 characters, it takes about 25 milliseconds to use regular expressions (conjecture, the time consumed is in the regular interpretation, while the other two methods are less than 1 milliseconds, and if the data amount is 80000 characters, it takes approximately 45 milliseconds to use the regular expression, while the other two methods are approximately 14 milliseconds.
With this test, regular expressions have great benefits in developing coding, reading and preventing bugs, but their performance is still noteworthy.