Is Regex faster?
-
Hi, if you had to search 50000 lines of code for the word example as fast as possible, with every kind of whitespace in the line, which method would you prefer. REGEX or myString.Trim() == "example"
May be Regex, which compiled into assembly, but I'm not sure. It's better to test.
-
Hi, if you had to search 50000 lines of code for the word example as fast as possible, with every kind of whitespace in the line, which method would you prefer. REGEX or myString.Trim() == "example"
error1408 wrote:
if you had to search 50000 lines of code for the word example as fast as possible, with every kind of whitespace in the line, which method would you prefer. REGEX or myString.Trim() == "example"
In this case RegEx would be the only one that works. The second example would only ever return false.
Upcoming FREE developer events: * Glasgow: SQL Server Managed Objects AND Reporting Services ... My website
-
Hi, if you had to search 50000 lines of code for the word example as fast as possible, with every kind of whitespace in the line, which method would you prefer. REGEX or myString.Trim() == "example"
string.IndexOf
[My Blog]
"Visual studio desperately needs some performance improvements. It is sometimes almost as slow as eclipse." - RĂ¼diger Klaehn
"Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe -
Hi, if you had to search 50000 lines of code for the word example as fast as possible, with every kind of whitespace in the line, which method would you prefer. REGEX or myString.Trim() == "example"
Why not try it out for yourself and find out. Create a document containing the phrase "The quick brown fox jumps over the lazy fox" by cutting and pasting repeatedly; it won't take you long to knock up a 50000 line document with this in. Now, put a seed word in the middle somewhere - call it seed for instance. Then, run a program that uses both versions against this document and see which method is faster.
Deja View - the feeling that you've seen this post before.
-
Why not try it out for yourself and find out. Create a document containing the phrase "The quick brown fox jumps over the lazy fox" by cutting and pasting repeatedly; it won't take you long to knock up a 50000 line document with this in. Now, put a seed word in the middle somewhere - call it seed for instance. Then, run a program that uses both versions against this document and see which method is faster.
Deja View - the feeling that you've seen this post before.
Ok i wrote a little app that compares the two methods. I found out, that testing a file with 60000 lines 30 times took my pc about a second with this method:
using (StreamReader rIn = new StreamReader("test.txt")) { string line = ""; while ((line = rIn.ReadLine()) != null) { if (line.Trim() == "{{{tcl") { return true; } } rIn.Close(); } return false;
and about 9 seconds with the regex:using (StreamReader rIn = new StreamReader("test.txt")) { string line = ""; Regex reg = new Regex(@"\s*\{\{\{tcl\s*"); while ((line = rIn.ReadLine()) != null) { if (reg.IsMatch(line)) { return true; } } rIn.Close(); } return false;
Thats a BIG difference. -
Ok i wrote a little app that compares the two methods. I found out, that testing a file with 60000 lines 30 times took my pc about a second with this method:
using (StreamReader rIn = new StreamReader("test.txt")) { string line = ""; while ((line = rIn.ReadLine()) != null) { if (line.Trim() == "{{{tcl") { return true; } } rIn.Close(); } return false;
and about 9 seconds with the regex:using (StreamReader rIn = new StreamReader("test.txt")) { string line = ""; Regex reg = new Regex(@"\s*\{\{\{tcl\s*"); while ((line = rIn.ReadLine()) != null) { if (reg.IsMatch(line)) { return true; } } rIn.Close(); } return false;
Thats a BIG difference.A well deserved 5 for answering your own question.
Deja View - the feeling that you've seen this post before.