<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Comparing RegEx.Replace, String.Replace and StringBuilder.Replace – Which has better performance?</title><link>http://blogs.msdn.com/debuggingtoolbox/archive/2008/04/02/comparing-regex-replace-string-replace-and-stringbuilder-replace-which-has-better-performance.aspx</link><description>A few days ago I was with Frank Taglianetti (no links here, he doesn’t have a blog yet), a PFE from my team that I met for the first time at that day while doing a Lab for one of our customers. By Lab I mean stress testing and troubleshooting a customer’s</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Comparing RegEx.Replace, String.Replace and StringBuilder.Replace – Which has better performance?</title><link>http://blogs.msdn.com/debuggingtoolbox/archive/2008/04/02/comparing-regex-replace-string-replace-and-stringbuilder-replace-which-has-better-performance.aspx#8351332</link><pubDate>Wed, 02 Apr 2008 16:54:32 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8351332</guid><dc:creator>Christopher_G_Lewis</dc:creator><description>&lt;p&gt;What was the size of your data set? &amp;nbsp;Did you try it over different size files? &amp;nbsp;SB should be much better for larger data sets, and I certainly wouldn't want to use a String if the size is over 85k (large object heap!)&lt;/p&gt;
&lt;p&gt;What about the memory footprint of each one? &amp;nbsp;String.Replace() is certainly going to leave a bunch of junk around that will need to be gc'd later. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;Basically, this is a worthless statistical blip that fits *just* your dataset, and shouldn't be applied as a one-size-fits-all analysis.&lt;/p&gt;</description></item><item><title>Interesting Finds: April 2, 2008</title><link>http://blogs.msdn.com/debuggingtoolbox/archive/2008/04/02/comparing-regex-replace-string-replace-and-stringbuilder-replace-which-has-better-performance.aspx#8351499</link><pubDate>Wed, 02 Apr 2008 17:26:34 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8351499</guid><dc:creator>Jason Haley</dc:creator><description /></item><item><title>re: Comparing RegEx.Replace, String.Replace and StringBuilder.Replace – Which has better performance?</title><link>http://blogs.msdn.com/debuggingtoolbox/archive/2008/04/02/comparing-regex-replace-string-replace-and-stringbuilder-replace-which-has-better-performance.aspx#8351796</link><pubDate>Wed, 02 Apr 2008 19:25:26 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8351796</guid><dc:creator>BlindWanderer</dc:creator><description>&lt;p&gt;Have you tried running it with the RegexOptions.Compiled compiled flag? You should get better performance.&lt;/p&gt;</description></item><item><title>re: Comparing RegEx.Replace, String.Replace and StringBuilder.Replace – Which has better performance?</title><link>http://blogs.msdn.com/debuggingtoolbox/archive/2008/04/02/comparing-regex-replace-string-replace-and-stringbuilder-replace-which-has-better-performance.aspx#8352104</link><pubDate>Wed, 02 Apr 2008 21:38:47 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8352104</guid><dc:creator>Roberto Farah</dc:creator><description>&lt;p&gt;Cristopher, we used files from 2 mb to over 6.5 mb. During the tests we inverted the sequence of execution. We haven't noticed anything abnormal with CPU. Like you, I was expecting to see SB surpassing the other two approaches. If you decide to try it and get different results, let me know, please.&lt;/p&gt;
&lt;p&gt;BlidWanderer, I haven't tried it, but it's definitely a good idea to try. Have you tried it? I'm curious! :)&lt;/p&gt;</description></item><item><title>re: Comparing RegEx.Replace, String.Replace and StringBuilder.Replace – Which has better performance?</title><link>http://blogs.msdn.com/debuggingtoolbox/archive/2008/04/02/comparing-regex-replace-string-replace-and-stringbuilder-replace-which-has-better-performance.aspx#8352416</link><pubDate>Thu, 03 Apr 2008 00:21:26 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8352416</guid><dc:creator>Joel "Jaykul" Bennett</dc:creator><description>&lt;p&gt;Sorry guys, but the test is wrong ...&lt;/p&gt;
&lt;p&gt;The line $fileContent.Replace(&amp;quot;\n&amp;quot;, &amp;quot;&amp;quot;) replaces *nothing* because \n is just a two character string in PowerShell. &amp;nbsp;What you MEANT to test is: $fileContent.Replace(&amp;quot;`n&amp;quot;, &amp;quot;&amp;quot;)&lt;/p&gt;
&lt;p&gt;On top of that, the string &amp;quot;\n&amp;quot; is a regular expression PATTERN, which means it requires pattern matching -- something you CANNOT DO with either of the other two methods. &amp;nbsp;You should try with &amp;quot;`n&amp;quot; in all three cases, that way you're just replacing a single character.&lt;/p&gt;
&lt;p&gt;As a side note, *removing* single characters isn't really that great of a test anyway -- you should try *replacing* a whole word with a word of a different length -- maybe try replacing &amp;quot;Visual Basic&amp;quot; in that MSDN article you linked to with &amp;quot;VB&amp;quot; or something...&lt;/p&gt;</description></item><item><title>re: Comparing RegEx.Replace, String.Replace and StringBuilder.Replace – Which has better performance?</title><link>http://blogs.msdn.com/debuggingtoolbox/archive/2008/04/02/comparing-regex-replace-string-replace-and-stringbuilder-replace-which-has-better-performance.aspx#8352478</link><pubDate>Thu, 03 Apr 2008 00:48:11 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8352478</guid><dc:creator>Roberto Farah</dc:creator><description>&lt;p&gt;Thanks for the correction! After testing with other special chars I made this mistake when rolling back to newline again.&lt;/p&gt;
&lt;p&gt;Anyway, after fixing it the proportion didn't change. As you can see below String.Replace is the fastest approach.&lt;/p&gt;
&lt;p&gt;We just replaced special characters, not words or regular characters, because that was the requirement for a particular function we were investigating. I totally agree with you that it's&lt;/p&gt;
&lt;p&gt;not a full stress test and I mentioned this in the article.&lt;/p&gt;
&lt;p&gt;Here are the results:&lt;/p&gt;
&lt;p&gt;RegEx&lt;/p&gt;
&lt;p&gt;PS C:\development\My Tools&amp;gt; measure-command { .\RegExReplacePerformance.ps1 test.txt }&lt;/p&gt;
&lt;p&gt;Starting RegEx.Replace...&lt;/p&gt;
&lt;p&gt;End!&lt;/p&gt;
&lt;p&gt;Days &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;: 0&lt;/p&gt;
&lt;p&gt;Hours &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; : 0&lt;/p&gt;
&lt;p&gt;Minutes &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; : 3&lt;/p&gt;
&lt;p&gt;Seconds &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; : 59 &amp;nbsp;&amp;lt;-- Here!&lt;/p&gt;
&lt;p&gt;Milliseconds &amp;nbsp; &amp;nbsp; &amp;nbsp;: 907&lt;/p&gt;
&lt;p&gt;Ticks &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; : 2399070288&lt;/p&gt;
&lt;p&gt;TotalDays &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; : 0.00277670172222222&lt;/p&gt;
&lt;p&gt;TotalHours &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;: 0.0666408413333333&lt;/p&gt;
&lt;p&gt;TotalMinutes &amp;nbsp; &amp;nbsp; &amp;nbsp;: 3.99845048&lt;/p&gt;
&lt;p&gt;TotalSeconds &amp;nbsp; &amp;nbsp; &amp;nbsp;: 239.9070288&lt;/p&gt;
&lt;p&gt;TotalMilliseconds : 239907.0288&lt;/p&gt;
&lt;p&gt;StringBuilder:&lt;/p&gt;
&lt;p&gt;PS C:\development\My Tools&amp;gt; measure-command { .\StringBuilderReplacePerformance.ps1 test.txt }&lt;/p&gt;
&lt;p&gt;Starting StringBuilder.Replace...&lt;/p&gt;
&lt;p&gt;End!&lt;/p&gt;
&lt;p&gt;Days &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;: 0&lt;/p&gt;
&lt;p&gt;Hours &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; : 0&lt;/p&gt;
&lt;p&gt;Minutes &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; : 0&lt;/p&gt;
&lt;p&gt;Seconds &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; : 34 &amp;nbsp;&amp;lt;-- Here!&lt;/p&gt;
&lt;p&gt;Milliseconds &amp;nbsp; &amp;nbsp; &amp;nbsp;: 570&lt;/p&gt;
&lt;p&gt;Ticks &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; : 345702214&lt;/p&gt;
&lt;p&gt;TotalDays &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; : 0.000400118303240741&lt;/p&gt;
&lt;p&gt;TotalHours &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;: 0.00960283927777778&lt;/p&gt;
&lt;p&gt;TotalMinutes &amp;nbsp; &amp;nbsp; &amp;nbsp;: 0.576170356666667&lt;/p&gt;
&lt;p&gt;TotalSeconds &amp;nbsp; &amp;nbsp; &amp;nbsp;: 34.5702214&lt;/p&gt;
&lt;p&gt;TotalMilliseconds : 34570.2214&lt;/p&gt;
&lt;p&gt;String:&lt;/p&gt;
&lt;p&gt;PS C:\development\My Tools&amp;gt; measure-command { .\StringReplacePerformance.ps1 test.txt }&lt;/p&gt;
&lt;p&gt;Starting String.Replace...&lt;/p&gt;
&lt;p&gt;End!&lt;/p&gt;
&lt;p&gt;Days &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;: 0&lt;/p&gt;
&lt;p&gt;Hours &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; : 0&lt;/p&gt;
&lt;p&gt;Minutes &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; : 0&lt;/p&gt;
&lt;p&gt;Seconds &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; : 20 &amp;nbsp;&amp;lt;-- Here!&lt;/p&gt;
&lt;p&gt;Milliseconds &amp;nbsp; &amp;nbsp; &amp;nbsp;: 583&lt;/p&gt;
&lt;p&gt;Ticks &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; : 205835077&lt;/p&gt;
&lt;p&gt;TotalDays &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; : 0.000238235042824074&lt;/p&gt;
&lt;p&gt;TotalHours &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;: 0.00571764102777778&lt;/p&gt;
&lt;p&gt;TotalMinutes &amp;nbsp; &amp;nbsp; &amp;nbsp;: 0.343058461666667&lt;/p&gt;
&lt;p&gt;TotalSeconds &amp;nbsp; &amp;nbsp; &amp;nbsp;: 20.5835077&lt;/p&gt;
&lt;p&gt;TotalMilliseconds : 20583.5077&lt;/p&gt;
&lt;p&gt;Thanks&lt;/p&gt;</description></item><item><title>Interesting Finds: 2008.04.03</title><link>http://blogs.msdn.com/debuggingtoolbox/archive/2008/04/02/comparing-regex-replace-string-replace-and-stringbuilder-replace-which-has-better-performance.aspx#8352824</link><pubDate>Thu, 03 Apr 2008 04:24:51 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8352824</guid><dc:creator>gOODiDEA</dc:creator><description>&lt;p&gt;.NET:WorkingwithEvents,part1AFast/CompactSerializationFrameworkHowtosetanIISApplica...&lt;/p&gt;
</description></item><item><title>Interesting Finds: 2008.04.03</title><link>http://blogs.msdn.com/debuggingtoolbox/archive/2008/04/02/comparing-regex-replace-string-replace-and-stringbuilder-replace-which-has-better-performance.aspx#8352825</link><pubDate>Thu, 03 Apr 2008 04:25:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8352825</guid><dc:creator>gOODiDEA.NET</dc:creator><description>&lt;p&gt;.NET: Working with Events, part 1 A Fast/Compact Serialization Framework How to set an IIS Application&lt;/p&gt;
</description></item><item><title>re: Comparing RegEx.Replace, String.Replace and StringBuilder.Replace – Which has better performance?</title><link>http://blogs.msdn.com/debuggingtoolbox/archive/2008/04/02/comparing-regex-replace-string-replace-and-stringbuilder-replace-which-has-better-performance.aspx#8353036</link><pubDate>Thu, 03 Apr 2008 06:59:58 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8353036</guid><dc:creator>Joel "Jaykul" Bennett</dc:creator><description>&lt;p&gt;It's me again ;-) there's still something wrong with that, because there's just no way the regex takes 8 times as long...&lt;/p&gt;
&lt;p&gt;If the code you're using is exactly what you pasted, the problem is that you're passing an array of strings to [regex]::replace instead of a string. (to be fair, you should also be assigning the output to a variable, although it won't make much difference if you use Measure-Command)&lt;/p&gt;
&lt;p&gt;try this: &lt;/p&gt;
&lt;p&gt;$lines = gc $fileName&lt;/p&gt;
&lt;p&gt;[string]$text = gc $fileName&lt;/p&gt;
&lt;p&gt;and then run your [regex]::replace on $lines and on $text ... the array takes MUCH longer (depending mostly on what you replace)&lt;/p&gt;
&lt;p&gt;My results (using the Get-PerformanceHistory script from PowerShellCentral.com/scripts):&lt;/p&gt;
&lt;p&gt;Duration Average Commmand&lt;/p&gt;
&lt;p&gt;-------- ------- --------&lt;/p&gt;
&lt;p&gt;13.01577 0.13016 1..100 | ForEach { $out1 = [regex]::replace($lines,&amp;quot;`n&amp;quot;,&amp;quot;&amp;quot;) }&lt;/p&gt;
&lt;p&gt; 3.21073 0.03211 1..100 | ForEach { $out2 = [regex]::replace($text,&amp;quot;`n&amp;quot;,&amp;quot;&amp;quot;) }&lt;/p&gt;
&lt;p&gt; 2.81232 0.02812 1..100 | ForEach { $out3 = $text.replace(&amp;quot;`n&amp;quot;,&amp;quot;&amp;quot;) }&lt;/p&gt;</description></item><item><title>re: Comparing RegEx.Replace, String.Replace and StringBuilder.Replace – Which has better performance?</title><link>http://blogs.msdn.com/debuggingtoolbox/archive/2008/04/02/comparing-regex-replace-string-replace-and-stringbuilder-replace-which-has-better-performance.aspx#8353049</link><pubDate>Thu, 03 Apr 2008 07:09:33 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8353049</guid><dc:creator>Joel "Jaykul" Bennett</dc:creator><description>&lt;p&gt;Incidentally ... I think it's clear by now that my corrections aren't meant to disprove the basic premise -- that string.replace is fastest -- it obviously is.&lt;/p&gt;
&lt;p&gt;The only reason I bother with the correction is that the difference is very minor, not a multiple.&lt;/p&gt;
&lt;p&gt;My point is just to make sure it's clear that under normal circumstances, you should be able to just use whatever format your string is already in -- because the time (and memory) it takes to convert from one to the other (and back?) is too big of an offset ;-)&lt;/p&gt;</description></item><item><title>re: Comparing RegEx.Replace, String.Replace and StringBuilder.Replace – Which has better performance?</title><link>http://blogs.msdn.com/debuggingtoolbox/archive/2008/04/02/comparing-regex-replace-string-replace-and-stringbuilder-replace-which-has-better-performance.aspx#8353120</link><pubDate>Thu, 03 Apr 2008 08:47:27 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8353120</guid><dc:creator>Roberto Farah</dc:creator><description>&lt;P&gt;Hi Joel,&lt;/P&gt;
&lt;P&gt;I'm using a text file that is, in fact, a License Agreement:&lt;/P&gt;
&lt;P&gt;"License Agreement&lt;/P&gt;
&lt;P&gt;This License Agreement describes the rights and responsibilities of anyone using XXX.&lt;/P&gt;
&lt;P&gt;THIS IS AN AGREEMENT BETWEEN YOU AND..."&lt;/P&gt;
&lt;P&gt;I copied and pasted it several times to create an even bigger file, not for the real tests, but for the blog tests.&lt;/P&gt;
&lt;P&gt;I didn't use [text] $var to avoid the implicit conversion to System.String, however, if I do that the time improves a lot, but doesn't beat String.Replace. Maybe I should've done it, as you said, to get closer results. (I'm putting a comment in the source code)&lt;/P&gt;
&lt;P&gt;Anyway I'm glad to know our results are the same! :) By the way, nice blog! I see you are a PowerShell Master! &lt;/P&gt;</description></item><item><title>re: Comparing RegEx.Replace, String.Replace and StringBuilder.Replace – Which has better performance?</title><link>http://blogs.msdn.com/debuggingtoolbox/archive/2008/04/02/comparing-regex-replace-string-replace-and-stringbuilder-replace-which-has-better-performance.aspx#8353408</link><pubDate>Thu, 03 Apr 2008 13:32:34 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8353408</guid><dc:creator>Lox</dc:creator><description>&lt;p&gt;StringBuilder.Replace returns 'this', not a new instance (like String.Replace). So in this place&lt;/p&gt;
&lt;p&gt;$fileContent.Append($str)&lt;/p&gt;
&lt;p&gt;for($i = 0; $i -le 200; $i++)&lt;/p&gt;
&lt;p&gt;{ &amp;nbsp; &lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;$builder = $fileContent.Replace(&amp;quot;`n&amp;quot;, &amp;quot;&amp;quot;) &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;for $i &amp;gt; 0, $fileContent does not contain &amp;quot;'n&amp;quot; - nothing to replace, searching only. IMHO, this fact can affect results.&lt;/p&gt;
&lt;p&gt;But.&lt;/p&gt;
&lt;p&gt;I've made the same things in C# (.NET 2.0), here is the code:&lt;/p&gt;
&lt;p&gt;static void Main(string[] args)&lt;/p&gt;
&lt;p&gt;		{&lt;/p&gt;
&lt;p&gt;			const int Runs = 200;&lt;/p&gt;
&lt;p&gt;			string fileData = null;&lt;/p&gt;
&lt;p&gt;			string result = null;&lt;/p&gt;
&lt;p&gt;			using (StreamReader reader = new StreamReader(&amp;quot;data.txt&amp;quot;, Encoding.GetEncoding(1251)))&lt;/p&gt;
&lt;p&gt;			{&lt;/p&gt;
&lt;p&gt;				fileData = reader.ReadToEnd();&lt;/p&gt;
&lt;p&gt;			}&lt;/p&gt;
&lt;p&gt;			Stopwatch timer = new Stopwatch();&lt;/p&gt;
&lt;p&gt;			/* RegEx Replace */&lt;/p&gt;
&lt;p&gt;			for (int run = 0; run &amp;lt; Runs; run++)&lt;/p&gt;
&lt;p&gt;			{&lt;/p&gt;
&lt;p&gt;				timer.Start();&lt;/p&gt;
&lt;p&gt;				result = Regex.Replace(fileData, &amp;quot;\n&amp;quot;, &amp;quot; &amp;quot;);&lt;/p&gt;
&lt;p&gt;				timer.Stop();&lt;/p&gt;
&lt;p&gt;			}&lt;/p&gt;
&lt;p&gt;			Console.WriteLine(&amp;quot;Regex.Replace - {0} ms&amp;quot;, timer.ElapsedMilliseconds);&lt;/p&gt;
&lt;p&gt;			timer.Reset();&lt;/p&gt;
&lt;p&gt;			/* StringBuilder.Replace */&lt;/p&gt;
&lt;p&gt;			StringBuilder builder = new StringBuilder();&lt;/p&gt;
&lt;p&gt;			for (int run = 0; run &amp;lt; Runs; run++)&lt;/p&gt;
&lt;p&gt;			{&lt;/p&gt;
&lt;p&gt;				builder.Append(fileData);&lt;/p&gt;
&lt;p&gt;				timer.Start();&lt;/p&gt;
&lt;p&gt;				builder.Replace(&amp;quot;\n&amp;quot;, &amp;quot;&amp;quot;);&lt;/p&gt;
&lt;p&gt;				timer.Stop();&lt;/p&gt;
&lt;p&gt;				builder.Remove(0, builder.Length);&lt;/p&gt;
&lt;p&gt;			}&lt;/p&gt;
&lt;p&gt;			Console.WriteLine(&amp;quot;StringBuilder.Replace - {0} ms&amp;quot;, timer.ElapsedMilliseconds);&lt;/p&gt;
&lt;p&gt;			timer.Reset();&lt;/p&gt;
&lt;p&gt;			/* String.Replace */&lt;/p&gt;
&lt;p&gt;			for (int run = 0; run &amp;lt; Runs; run++)&lt;/p&gt;
&lt;p&gt;			{&lt;/p&gt;
&lt;p&gt;				timer.Start();&lt;/p&gt;
&lt;p&gt;				result = fileData.Replace(&amp;quot;\n&amp;quot;, &amp;quot;&amp;quot;);&lt;/p&gt;
&lt;p&gt;				timer.Stop();&lt;/p&gt;
&lt;p&gt;			}&lt;/p&gt;
&lt;p&gt;			Console.WriteLine(&amp;quot;String.Replace - {0} ms&amp;quot;, timer.ElapsedMilliseconds);&lt;/p&gt;
&lt;p&gt;			timer.Reset();&lt;/p&gt;
&lt;p&gt;			Console.ReadKey();&lt;/p&gt;
&lt;p&gt;		}&lt;/p&gt;
&lt;p&gt;I've got the following results:&lt;/p&gt;
&lt;p&gt;Regex.Replace - 3984 ms&lt;/p&gt;
&lt;p&gt;StringBuilder.Replace - 1691 ms&lt;/p&gt;
&lt;p&gt;String.Replace - 2108 ms&lt;/p&gt;
&lt;p&gt;Something's wrong?&lt;/p&gt;</description></item><item><title>re: Comparing RegEx.Replace, String.Replace and StringBuilder.Replace – Which has better performance?</title><link>http://blogs.msdn.com/debuggingtoolbox/archive/2008/04/02/comparing-regex-replace-string-replace-and-stringbuilder-replace-which-has-better-performance.aspx#8354709</link><pubDate>Thu, 03 Apr 2008 21:56:27 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8354709</guid><dc:creator>Roberto Farah</dc:creator><description>&lt;p&gt;Interesting... your results are different from mine, Joel and &lt;a rel="nofollow" target="_new" href="http://www.codeproject.com/KB/cs/StringBuilder_vs_String.aspx?fid=326464&amp;amp;df=90&amp;amp;mpp=25&amp;amp;noise=3&amp;amp;sort=Position&amp;amp;view=Quick&amp;amp;fr=26"&gt;http://www.codeproject.com/KB/cs/StringBuilder_vs_String.aspx?fid=326464&amp;amp;df=90&amp;amp;mpp=25&amp;amp;noise=3&amp;amp;sort=Position&amp;amp;view=Quick&amp;amp;fr=26&lt;/a&gt; that uses C#. &amp;nbsp;(I haven't tested your code in my machine)&lt;/p&gt;
&lt;p&gt;I'm wonder if someone else has also tested the three approaches in PowerShell or C#/VB.NET. I'm curious to see which approach is the fastest and if there is a consistent winner. &amp;nbsp;:)&lt;/p&gt;</description></item><item><title>re: Comparing RegEx.Replace, String.Replace and StringBuilder.Replace – Which has better performance?</title><link>http://blogs.msdn.com/debuggingtoolbox/archive/2008/04/02/comparing-regex-replace-string-replace-and-stringbuilder-replace-which-has-better-performance.aspx#8355067</link><pubDate>Fri, 04 Apr 2008 00:41:09 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8355067</guid><dc:creator>Niki</dc:creator><description>&lt;p&gt;Did you try replacing longer strings? IIRC Regex uses Boyer-Moore for matching, which should be more efficient for longer patterns. Of course, this only affects the time for searching, not the time needed for replacing the string, so it might also depend on how often the pattern has to be replaced. And since you're using the default locale, the results might be completely different (think factor 10!) on a machine running with a different locale.&lt;/p&gt;</description></item><item><title>re: Comparing RegEx.Replace, String.Replace and StringBuilder.Replace – Which has better performance?</title><link>http://blogs.msdn.com/debuggingtoolbox/archive/2008/04/02/comparing-regex-replace-string-replace-and-stringbuilder-replace-which-has-better-performance.aspx#8355891</link><pubDate>Fri, 04 Apr 2008 09:53:41 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8355891</guid><dc:creator>Roberto Farah</dc:creator><description>&lt;p&gt;I didn't try to replace longer strings. I haven't investigated the locale, but thanks for pointing it. I'm wondering if the locale should affect the results, not the raw numbers from tests, but which is the fastest approach. My guess (and this is just a wild guess :) ) is that String.Replace should continue to be the fastest approach, because all times would be equally impacted after changing the locale.&lt;/p&gt;</description></item><item><title>re: Comparing RegEx.Replace, String.Replace and StringBuilder.Replace – Which has better performance?</title><link>http://blogs.msdn.com/debuggingtoolbox/archive/2008/04/02/comparing-regex-replace-string-replace-and-stringbuilder-replace-which-has-better-performance.aspx#8607080</link><pubDate>Mon, 16 Jun 2008 23:45:39 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8607080</guid><dc:creator>Mark Bussey</dc:creator><description>&lt;p&gt;Have you run the CLR profiler on these three methods? &amp;nbsp;String objects seem to hang around, clogging up the works, much longer that StringBuilder objects.&lt;/p&gt;</description></item><item><title>re: Comparing RegEx.Replace, String.Replace and StringBuilder.Replace – Which has better performance?</title><link>http://blogs.msdn.com/debuggingtoolbox/archive/2008/04/02/comparing-regex-replace-string-replace-and-stringbuilder-replace-which-has-better-performance.aspx#8607222</link><pubDate>Tue, 17 Jun 2008 00:09:13 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8607222</guid><dc:creator>Roberto Farah</dc:creator><description>&lt;p&gt;No, I haven't. Did you get different results? &lt;/p&gt;</description></item><item><title>re: Comparing RegEx.Replace, String.Replace and StringBuilder.Replace – Which has better performance?</title><link>http://blogs.msdn.com/debuggingtoolbox/archive/2008/04/02/comparing-regex-replace-string-replace-and-stringbuilder-replace-which-has-better-performance.aspx#9858277</link><pubDate>Wed, 05 Aug 2009 21:04:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9858277</guid><dc:creator>William Wegerson</dc:creator><description>&lt;p&gt;As much as I *love* regex...the situation presented doesn't speak to why one would even think of using regex for that work. Replacing a character is strictly the realm of string/stringbuilder replace and optimized accordingly. &lt;/p&gt;
&lt;p&gt;Regex is meant for complicated patterns and has overhead to that end. But the community slams regex due to that overhead. My point is this article as read by the blogsphere, reads that the implication that regex is slow for everything. Factually in this test its slower but there are issues not being considered; hence regex gets painted with a broad stroke.&lt;/p&gt;
&lt;p&gt;If one presented a scenario where the replacement need was really a complex situation. I would bet that regex could easily speak to that and provide a good showing. For the *added overhead* of the support or multiple calls needed to use string/stringbuilder replace would begin to slow down and show regex pulling ahead. &lt;/p&gt;
&lt;p&gt;Long story short; use the right tool for the right situation. Regex is a truck and used to haul large quantities and string/stringbuilder are sport cars quick but not meant to carry large loads. &lt;/p&gt;
&lt;p&gt;Thanks for the article though...it is interesting.&lt;/p&gt;</description></item><item><title>re: Comparing RegEx.Replace, String.Replace and StringBuilder.Replace – Which has better performance?</title><link>http://blogs.msdn.com/debuggingtoolbox/archive/2008/04/02/comparing-regex-replace-string-replace-and-stringbuilder-replace-which-has-better-performance.aspx#9858784</link><pubDate>Thu, 06 Aug 2009 09:34:35 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9858784</guid><dc:creator>talav</dc:creator><description>&lt;p&gt;Regex.Replace - &amp;nbsp;ms1148&lt;/p&gt;
&lt;p&gt;StringBuilder.Replace - &amp;nbsp;ms248&lt;/p&gt;
&lt;p&gt;String.Replace - &amp;nbsp;ms263&lt;/p&gt;
&lt;p&gt;Same results as Lox &lt;/p&gt;</description></item></channel></rss>