<?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>Programming tidbits: store &amp; retrieve : Performance</title><link>http://blogs.msdn.com/oanapl/archive/tags/Performance/default.aspx</link><description>Tags: Performance</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Performance Comparison: Regex versus string operations</title><link>http://blogs.msdn.com/oanapl/archive/2009/04/04/performance-comparison-regex-versus-string-operations.aspx</link><pubDate>Sat, 04 Apr 2009 23:25:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9531533</guid><dc:creator>OanaPlaton</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/oanapl/comments/9531533.aspx</comments><wfw:commentRss>http://blogs.msdn.com/oanapl/commentrss.aspx?PostID=9531533</wfw:commentRss><description>&lt;P&gt;I consider &lt;A href="http://www.regular-expressions.info/" mce_href="http://www.regular-expressions.info/"&gt;regular expressions&lt;/A&gt; one of the most useful features ever. I use them a lot, not only when coding, but also when editing files and instead of copy and paste. I find the Visual Studio Find/Replace feature with regular expressions really useful as well. In case you are not familiar with it, you can use regular expressions to find and replace characters like this:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/oanapl/WindowsLiveWriter/PerformanceComparisonRegexversusstringco_B123/image_4.png" mce_href="http://blogs.msdn.com/blogfiles/oanapl/WindowsLiveWriter/PerformanceComparisonRegexversusstringco_B123/image_4.png"&gt;&lt;IMG style="BORDER-RIGHT: 0px; BORDER-TOP: 0px; BORDER-LEFT: 0px; BORDER-BOTTOM: 0px" height=282 alt=image src="http://blogs.msdn.com/blogfiles/oanapl/WindowsLiveWriter/PerformanceComparisonRegexversusstringco_B123/image_thumb_1.png" width=290 border=0 mce_src="http://blogs.msdn.com/blogfiles/oanapl/WindowsLiveWriter/PerformanceComparisonRegexversusstringco_B123/image_thumb_1.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;In the picture, I used the expression &lt;STRONG&gt;{[^;]+}; &lt;/STRONG&gt;- meaning tag the string formed by any characters until ";" (at least one character) and replace the matching text with "// " followed by the tagged expression, forgetting the last ";". There are a lot of tutorials about regular expressions. I just learned the basics, and now I just try, fail, undo, and try again until I get it right.&lt;/P&gt;
&lt;P&gt;Moving back to coding, &lt;A href="http://msdn.microsoft.com/en-us/library/hs600312(VS.71).aspx" mce_href="http://msdn.microsoft.com/en-us/library/hs600312(VS.71).aspx"&gt;.NET has great support for regular expressions&lt;/A&gt;. The classes are relatively easy to use (though at the beginning I had to play a while to find out how to capture strings in one match and other more advanced features). The biggest advantages I find in using Regex are that it makes parsing input very easy (once you have the regular expression in place) and it makes it much harder to introduce bugs - less code has by definition fewer bugs, and parsing with regular expressions requires less code than the traditional method of parsing strings with different string methods like get substring at different indexes, check that is starts or ends with certain characters etc. &lt;/P&gt;
&lt;P&gt;However, there are cases when the string concatenation and parsing is better than the regular expressions: when the checks are done on a path that is executed a lot (a hot path), and that has strict performance requirements. Why? The regular expressions are slower than string concatenation. &lt;/P&gt;
&lt;P&gt;I did a simple experiment and measured the time needed by regex and strings to perform the same operations. I considered I need to keep data about persons in the format "Firstname:Oana Lastname:Platon Money:2183 UniqueIdentifier:fwsjfjehfjkwh8r378". I have defined a constant that represents this format, and I'll use it to serialize the person data.&lt;/P&gt;
&lt;DIV style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 4pt; BORDER-TOP: windowtext 1pt solid; PADDING-LEFT: 4pt; BACKGROUND: #f2f2f2; PADDING-BOTTOM: 1pt; BORDER-LEFT: windowtext 1pt solid; PADDING-TOP: 1pt; BORDER-BOTTOM: windowtext 1pt solid; mso-element: para-border-div; mso-shading: white; mso-shading-themecolor: background1; mso-pattern: gray-5 auto; mso-border-shadow: yes"&gt;
&lt;P class=Ccode style="BACKGROUND: #f2f2f2; MARGIN: 0in 0in 0pt; mso-shading: white; mso-shading-themecolor: background1; mso-pattern: gray-5 auto"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;const&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; nameFormat = &lt;SPAN style="COLOR: #a31515"&gt;"Firstname:{0} Lastname:{1} Money:{2} UniqueIdentifier:{3}"&lt;/SPAN&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;/DIV&gt;
&lt;P&gt;The data must be serialized and deserialized a lot of times (lets say that we need to send the data on the wire frequently or something like that). When deserializing the data, we need to make sure that it respects the pattern and then we need to extract the firstname, lastname etc.&lt;/P&gt;
&lt;P&gt;1. Using &lt;A href="http://msdn.microsoft.com/en-us/library/30wbz966(VS.71).aspx" mce_href="http://msdn.microsoft.com/en-us/library/30wbz966(VS.71).aspx"&gt;regular expressions&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;I defined a regular expression like this:&lt;/P&gt;
&lt;DIV style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 4pt; BORDER-TOP: windowtext 1pt solid; PADDING-LEFT: 4pt; BACKGROUND: #f2f2f2; PADDING-BOTTOM: 1pt; BORDER-LEFT: windowtext 1pt solid; PADDING-TOP: 1pt; BORDER-BOTTOM: windowtext 1pt solid; mso-element: para-border-div; mso-shading: white; mso-shading-themecolor: background1; mso-pattern: gray-5 auto; mso-border-shadow: yes"&gt;
&lt;P class=Ccode style="BACKGROUND: #f2f2f2; MARGIN: 0in 0in 0pt; mso-shading: white; mso-shading-themecolor: background1; mso-pattern: gray-5 auto"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;static&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Regex&lt;/SPAN&gt; regex = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Regex&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"^Firstname:(\\w+)\\sLastname:(\\w+)\\sMoney:(\\d{1,9})\\sUniqueIdentifier:([\\w-]+)$"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #2b91af"&gt;RegexOptions&lt;/SPAN&gt;.IgnoreCase | &lt;SPAN style="COLOR: #2b91af"&gt;RegexOptions&lt;/SPAN&gt;.Compiled);&lt;/FONT&gt;&lt;/P&gt;&lt;/DIV&gt;
&lt;P&gt;Then, the code to parse the expressions and get the desired data is:&lt;/P&gt;
&lt;DIV style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 4pt; BORDER-TOP: windowtext 1pt solid; PADDING-LEFT: 4pt; BACKGROUND: #f2f2f2; PADDING-BOTTOM: 1pt; BORDER-LEFT: windowtext 1pt solid; PADDING-TOP: 1pt; BORDER-BOTTOM: windowtext 1pt solid; mso-element: para-border-div; mso-shading: white; mso-shading-themecolor: background1; mso-pattern: gray-5 auto; mso-border-shadow: yes"&gt;
&lt;P class=Ccode style="BACKGROUND: #f2f2f2; MARGIN: 0in 0in 0pt; mso-shading: white; mso-shading-themecolor: background1; mso-pattern: gray-5 auto"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; ParseWithRegex(&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; description)&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Ccode style="BACKGROUND: #f2f2f2; MARGIN: 0in 0in 0pt; mso-shading: white; mso-shading-themecolor: background1; mso-pattern: gray-5 auto"&gt;&lt;FONT face="Courier New"&gt;{&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Ccode style="BACKGROUND: #f2f2f2; MARGIN: 0in 0in 0pt; mso-shading: white; mso-shading-themecolor: background1; mso-pattern: gray-5 auto"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Match&lt;/SPAN&gt; m = regex.Match(description);&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Ccode style="BACKGROUND: #f2f2f2; MARGIN: 0in 0in 0pt; mso-shading: white; mso-shading-themecolor: background1; mso-pattern: gray-5 auto"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (!m.Success)&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Ccode style="BACKGROUND: #f2f2f2; MARGIN: 0in 0in 0pt; mso-shading: white; mso-shading-themecolor: background1; mso-pattern: gray-5 auto"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;{&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Ccode style="BACKGROUND: #f2f2f2; MARGIN: 0in 0in 0pt; mso-shading: white; mso-shading-themecolor: background1; mso-pattern: gray-5 auto"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;throw&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ArgumentException&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"description doesn't follow the expected format"&lt;/SPAN&gt;);&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Ccode style="BACKGROUND: #f2f2f2; MARGIN: 0in 0in 0pt; mso-shading: white; mso-shading-themecolor: background1; mso-pattern: gray-5 auto"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;}&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Ccode style="BACKGROUND: #f2f2f2; MARGIN: 0in 0in 0pt; mso-shading: white; mso-shading-themecolor: background1; mso-pattern: gray-5 auto"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.firstname = m.Groups[1].Value;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Ccode style="BACKGROUND: #f2f2f2; MARGIN: 0in 0in 0pt; mso-shading: white; mso-shading-themecolor: background1; mso-pattern: gray-5 auto"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.lastname = m.Groups[2].Value;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Ccode style="BACKGROUND: #f2f2f2; MARGIN: 0in 0in 0pt; mso-shading: white; mso-shading-themecolor: background1; mso-pattern: gray-5 auto"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (!&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;.TryParse(m.Groups[3].Value, &lt;SPAN style="COLOR: blue"&gt;out&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.age))&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Ccode style="BACKGROUND: #f2f2f2; MARGIN: 0in 0in 0pt; mso-shading: white; mso-shading-themecolor: background1; mso-pattern: gray-5 auto"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;{&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Ccode style="BACKGROUND: #f2f2f2; MARGIN: 0in 0in 0pt; mso-shading: white; mso-shading-themecolor: background1; mso-pattern: gray-5 auto"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;throw&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ArgumentException&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"age doesn't have the correct value"&lt;/SPAN&gt;);&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Ccode style="BACKGROUND: #f2f2f2; MARGIN: 0in 0in 0pt; mso-shading: white; mso-shading-themecolor: background1; mso-pattern: gray-5 auto"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;}&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Ccode style="BACKGROUND: #f2f2f2; MARGIN: 0in 0in 0pt; mso-shading: white; mso-shading-themecolor: background1; mso-pattern: gray-5 auto"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.uniqueIdentifier = m.Groups[4].Value;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Ccode style="BACKGROUND: #f2f2f2; MARGIN: 0in 0in 0pt; mso-shading: white; mso-shading-themecolor: background1; mso-pattern: gray-5 auto"&gt;&lt;FONT face="Courier New"&gt;}&lt;/FONT&gt;&lt;/P&gt;&lt;/DIV&gt;
&lt;P&gt;2. Using string operations &lt;/P&gt;
&lt;P&gt;The verification that the&amp;nbsp;given string respects the format becomes more difficult. In our case, the patters is pretty simple, but imagine that we&amp;nbsp;needed to check an email address or something more complicated. In that case, the code would have had a lot of cases, to follow all possible solutions. &lt;/P&gt;
&lt;DIV style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 4pt; BORDER-TOP: windowtext 1pt solid; PADDING-LEFT: 4pt; BACKGROUND: #f2f2f2; PADDING-BOTTOM: 1pt; BORDER-LEFT: windowtext 1pt solid; PADDING-TOP: 1pt; BORDER-BOTTOM: windowtext 1pt solid; mso-element: para-border-div; mso-shading: white; mso-shading-themecolor: background1; mso-pattern: gray-5 auto; mso-border-shadow: yes"&gt;
&lt;P class=Ccode style="BACKGROUND: #f2f2f2; MARGIN: 0in 0in 0pt; mso-shading: white; mso-shading-themecolor: background1; mso-pattern: gray-5 auto"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; ParseWithStrings(&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; description)&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Ccode style="BACKGROUND: #f2f2f2; MARGIN: 0in 0in 0pt; mso-shading: white; mso-shading-themecolor: background1; mso-pattern: gray-5 auto"&gt;&lt;FONT face="Courier New"&gt;{&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Ccode style="BACKGROUND: #f2f2f2; MARGIN: 0in 0in 0pt; mso-shading: white; mso-shading-themecolor: background1; mso-pattern: gray-5 auto"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;[] parts = description.Split(&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;char&lt;/SPAN&gt;[] { &lt;SPAN style="COLOR: #a31515"&gt;' '&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #a31515"&gt;'\t'&lt;/SPAN&gt; });&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Ccode style="BACKGROUND: #f2f2f2; MARGIN: 0in 0in 0pt; mso-shading: white; mso-shading-themecolor: background1; mso-pattern: gray-5 auto"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (parts.Length != 4)&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Ccode style="BACKGROUND: #f2f2f2; MARGIN: 0in 0in 0pt; mso-shading: white; mso-shading-themecolor: background1; mso-pattern: gray-5 auto"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;{&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Ccode style="BACKGROUND: #f2f2f2; MARGIN: 0in 0in 0pt; mso-shading: white; mso-shading-themecolor: background1; mso-pattern: gray-5 auto"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;throw&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ArgumentException&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"description doesn't follow the expected pattern"&lt;/SPAN&gt;);&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Ccode style="BACKGROUND: #f2f2f2; MARGIN: 0in 0in 0pt; mso-shading: white; mso-shading-themecolor: background1; mso-pattern: gray-5 auto"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;}&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Ccode style="BACKGROUND: #f2f2f2; MARGIN: 0in 0in 0pt; mso-shading: white; mso-shading-themecolor: background1; mso-pattern: gray-5 auto"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.firstname = parts[0].Substring(parts[0].IndexOf(&lt;SPAN style="COLOR: #a31515"&gt;":"&lt;/SPAN&gt;) + 1);&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Ccode style="BACKGROUND: #f2f2f2; MARGIN: 0in 0in 0pt; mso-shading: white; mso-shading-themecolor: background1; mso-pattern: gray-5 auto"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.lastname = parts[1].Substring(parts[1].IndexOf(&lt;SPAN style="COLOR: #a31515"&gt;":"&lt;/SPAN&gt;) + 1);&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Ccode style="BACKGROUND: #f2f2f2; MARGIN: 0in 0in 0pt; mso-shading: white; mso-shading-themecolor: background1; mso-pattern: gray-5 auto"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (!&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;.TryParse(parts[2].Substring(parts[2].IndexOf(&lt;SPAN style="COLOR: #a31515"&gt;":"&lt;/SPAN&gt;) + 1), &lt;SPAN style="COLOR: blue"&gt;out&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.age))&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Ccode style="BACKGROUND: #f2f2f2; MARGIN: 0in 0in 0pt; mso-shading: white; mso-shading-themecolor: background1; mso-pattern: gray-5 auto"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;{&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Ccode style="BACKGROUND: #f2f2f2; MARGIN: 0in 0in 0pt; mso-shading: white; mso-shading-themecolor: background1; mso-pattern: gray-5 auto"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;throw&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ArgumentException&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"age doesn't have the correct value"&lt;/SPAN&gt;);&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Ccode style="BACKGROUND: #f2f2f2; MARGIN: 0in 0in 0pt; mso-shading: white; mso-shading-themecolor: background1; mso-pattern: gray-5 auto"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;}&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Ccode style="BACKGROUND: #f2f2f2; MARGIN: 0in 0in 0pt; mso-shading: white; mso-shading-themecolor: background1; mso-pattern: gray-5 auto"&gt;&lt;FONT face="Courier New"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.uniqueIdentifier = parts[3].Substring(parts[3].IndexOf(&lt;SPAN style="COLOR: #a31515"&gt;":"&lt;/SPAN&gt;) + 1); ;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=Ccode style="BACKGROUND: #f2f2f2; MARGIN: 0in 0in 0pt; mso-shading: white; mso-shading-themecolor: background1; mso-pattern: gray-5 auto"&gt;&lt;FONT face="Courier New"&gt;}&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;/DIV&gt;
&lt;P&gt;See that this is much more error prone than the previous code, because it needs to look at a lot of indexes and to substract the desired part of the string.&lt;/P&gt;
&lt;P&gt;However, when I run the 2 methods in a loop and I measure how long they take with a stopwatch (from System.Diagnostics namespace), I get these results:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://blogs.msdn.com/blogfiles/oanapl/WindowsLiveWriter/PerformanceComparisonRegexversusstringco_B123/image_8.png" mce_href="http://blogs.msdn.com/blogfiles/oanapl/WindowsLiveWriter/PerformanceComparisonRegexversusstringco_B123/image_8.png"&gt;&lt;IMG style="BORDER-RIGHT: 0px; BORDER-TOP: 0px; BORDER-LEFT: 0px; BORDER-BOTTOM: 0px" height=260 alt=image src="http://blogs.msdn.com/blogfiles/oanapl/WindowsLiveWriter/PerformanceComparisonRegexversusstringco_B123/image_thumb_3.png" width=374 border=0 mce_src="http://blogs.msdn.com/blogfiles/oanapl/WindowsLiveWriter/PerformanceComparisonRegexversusstringco_B123/image_thumb_3.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;In conclusion, when choosing between using traditional string parsing or regular expressions, I would recommend:&lt;/P&gt;
&lt;P&gt;- Start with regular expressions; if the path is not a hot path and doesn't have any strict performance requirements, this is the best choice, since it makes the code easier to read and maintain.&lt;/P&gt;
&lt;P&gt;- If the performance goals are not reached (which means you have performance goals and you measured the performance!), try to improve your regular expressions. For example, adding ^ and $ (to specify that the pattern you are looking for is at the beginning or at the end of the string) when appropriate can improve the performance a lot. Also, make sure you use compiled expressions (if possible).&lt;/P&gt;
&lt;P&gt;- If you are still not in graphic, replace the regular expressions with string concatenation and parsing.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9531533" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/oanapl/archive/tags/Visual+Studio/default.aspx">Visual Studio</category><category domain="http://blogs.msdn.com/oanapl/archive/tags/Performance/default.aspx">Performance</category><category domain="http://blogs.msdn.com/oanapl/archive/tags/C_2300_/default.aspx">C#</category></item></channel></rss>