Which one is faster?
Regex.Replace(textBox1.Text, @"\r\n", "<br>", RegexOptions.IgnoreCase);
vs
textBox1.Text.Replace("\r\n","<br>");
Loading User Information from Channel 9
Something went wrong getting user information from Channel 9
Loading User Information from MSDN
Something went wrong getting user information from MSDN
Loading Visual Studio Achievements
Something went wrong getting the Visual Studio Achievements
Which one is faster?
Regex.Replace(textBox1.Text, @"\r\n", "<br>", RegexOptions.IgnoreCase);
vs
textBox1.Text.Replace("\r\n","<br>");
The replace since it's a hardcoded string function.
The Regex will take longer since it needs to parse the regex string (even if you set RegexOptions.Compiled) then execute it in the regex string
then formulate the resultant string. But if you really want to be sure, perform each iteration in a million-times iterator and time the result.
P.S. it's "<br />" not "<br>", and verbatim strings @"" don't support escape characters also they're not recognised by the regex engine.
Thanks! I keep forgetting it's <br />W3bbo said:The replace since it's a hardcoded string function.
The Regex will take longer since it needs to parse the regex string (even if you set RegexOptions.Compiled) then execute it in the regex string then formulate the resultant string. But if you really want to be sure, perform each iteration in a million-times iterator and time the result.
P.S. it's "<br />" not "<br>", and verbatim strings @"" don't support escape characters also they're not recognised by the regex engine.
> verbatim strings @"" don't support escape characters also they're not recognised by the regex engine.W3bbo said:The replace since it's a hardcoded string function.
The Regex will take longer since it needs to parse the regex string (even if you set RegexOptions.Compiled) then execute it in the regex string then formulate the resultant string. But if you really want to be sure, perform each iteration in a million-times iterator and time the result.
P.S. it's "<br />" not "<br>", and verbatim strings @"" don't support escape characters also they're not recognised by the regex engine.
Oh, I forgot that the Regex in .NET also recognises C-style escapes for newlines and carriage returns.Matthew van Eerde said:> verbatim strings @"" don't support escape characters also they're not recognised by the regex engine.W3bbo said:*snip*
Huh? The regex expects a string of the form "backslash, r, backslash, n".
Replace expects a string of the form "carriage return, line feed".
So the use of verbatim is correct here.
<br/> is only necessary in XHTML. Regular HTML does not need the backslash.W3bbo said:The replace since it's a hardcoded string function.
The Regex will take longer since it needs to parse the regex string (even if you set RegexOptions.Compiled) then execute it in the regex string then formulate the resultant string. But if you really want to be sure, perform each iteration in a million-times iterator and time the result.
P.S. it's "<br />" not "<br>", and verbatim strings @"" don't support escape characters also they're not recognised by the regex engine.
Normal html won't have issues with elements that properly terminate theirself though right? personally I'd always stick to doing html like xml.. elements should either self terminate properly or have a terminating end tag, this way you just remember to always do it one way.. and right.TommyCarlier said:<br/> is only necessary in XHTML. Regular HTML does not need the backslash.W3bbo said:*snip*
Yes, but you might as well be future-proof. XML-style self-terminating elements work fine in pretty much every SGML parser out there (according to the SGML specification the trailing '/' should be interpreted as a boolean attribute).TommyCarlier said:<br/> is only necessary in XHTML. Regular HTML does not need the backslash.W3bbo said:*snip*
Most importantly, if this is a function that is called on one textbox, or even a handful, with a non-gigantic amount of text, the difference in speed is irrelevant. Both will be far below anything the user will notice. If this is UI code you're talking about, I wouldn't bother wasting time optimizing this action.
I second this concept. Unless the comparison is going to happen millions of times the speed difference isn't necessarily important. Premature optimization is always a bad idea.Yggdrasil said:Most importantly, if this is a function that is called on one textbox, or even a handful, with a non-gigantic amount of text, the difference in speed is irrelevant. Both will be far below anything the user will notice. If this is UI code you're talking about, I wouldn't bother wasting time optimizing this action.
On the other hand string.Replace(string, string) is semantically easier to understand than the Regex equivilent.JPBuckeye said:I second this concept. Unless the comparison is going to happen millions of times the speed difference isn't necessarily important. Premature optimization is always a bad idea.Yggdrasil said:*snip*
Given two ways of doing something, regex is almost always slower.
I agree that regex is very helpful but works very slowly and if it is possible just try to avoid using it.Programous said:Given two ways of doing something, regex is almost always slower.
Thread Closed
This thread is kinda stale and has been closed but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.