Re: StringBuilder Extension: IsQuotedBy
Daniel Petersson <[email protected]> Thu, 14 Feb 2008 11:31:17 +0100
| Newsgroups | gmane.comp.windows.devel.dotnet.clr |
|---|---|
| Message-ID | <E8FBCCA96548DD46A1AC748F17EEAD9C0E9A464C29@cefalo-mail01.cefalo.local> |
perf, strings and loops I have developed a few .NET based parsers during the last few years and here are som fast comments: 1. avoid creating objects, all kinds of objects are "really expensive" to create, especially in loops. gc:s generally aren't an issue when parsing, unless parsing HUGE documents, a small parser will have completed before the gc kicks in. 2. avoid virtual calls and delegates; both are great for flexibility but slows your code down considerably when executed in tight parse-loops. 3. regexp really rocks if you are looking for flexibilty; in those cases where flexibility is more important then speed regexp is really the perfect match, but if you are working on a small, well-defined and really fast parser don't even think about regexp. (1), (2) and (3) all trade performance against readability and maintainability; I recomend regexp for all "slow" cases but if you really are looking for performance it isn't a good solution. If a parse case is "slow" or "fast" really depend on your development task, but be adviced, developing complex parsers without regexp isn't for the faint-hearted =) regards, Daniel ________________________________________ From: Discussion of development on the .NET platform using any managed language [[email protected]] On Behalf Of Brady Kelly [[email protected]] Sent: Thursday, February 14, 2008 8:58 AM To: [email protected] Subject: Re: [DOTNET-CLR] StringBuilder Extension: IsQuotedBy Probably, but I always get uneasy creating strings in loops, and this was just the culmination of my paranoia, fuelled by an evil combination of caffeine and fatigue. > > Isn't a ToString() with a regexp more efficient? > > FB > > > I was playing around with avoiding, at all costs, creating surplus > strings, > > and came up with the following method to see if a StringBuilder, that > I use > > for each of a collection of string fields, is surrounded by a certain > > string, e.g. double quotes. I was just wondering if this is an > efficiant > > way of doing it: > > > > > > > > public static bool IsQuotedBy(this StringBuilder sb, string > > quoteString) > > > > { > > > > // Check if the first characters match the quote string. > > > > for (int i = 0; i < quoteString.Length; i++) > > > > { > > > > if (sb[i] != quoteString[i]) > > > > { > > > > return false; > > > > } > > > > } > > > > > > > > // Check if the last characters match the quote string. > > > > for (int i = sb.Length - quoteString.Length; i < > sb.Length; i++ > > ) > > > > { > > > > int qsIndex = 0; > > > > if (sb[i] != quoteString[qsIndex++]) > > > > { > > > > return false; > > > > } > > > > } > > > > > > > > return true; > > > > } =================================== This list is hosted by DevelopMentor? http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com =================================== This list is hosted by DevelopMentorĀ® http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com