Fast string operations
- From: "Chad Myers" <cmyers@xxxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 01 Jun 2005 19:57:50 GMT
I've been perf testing an application of mine and I've noticed that there
are a lot (and I mean A LOT -- megabytes and megabytes of 'em) System.String
instances being created.
I've done some analysis and I'm led to believe (but can't yet quantitatively
establish as fact) that the two basic culprits are a lot of calls to:
1.) if( someString.ToLower() == "somestring" )
and
2.) if( someString != null && someString.Trim().Length > 0 )
ToLower() generates a new string instance as does Trim().
I believe that these are getting called many times and churning up a bunch
of strings faster than the GC can collect them, or perhaps there's some
weird interning/caching thing going on. Regardless, the number of string
instances grows and grows. It gets bumped down occasionally, but it's
basically 5 steps forward, 1 back.
For reference, this is an ASP application calling into .NET ComVisible
objects. So I assume this uses the workstation GC, right?
Anyhow, so I think that I can solve problem (1) with String.Compare() which
can perform in-place case-insensitive comparisons without generating new
string instances.
Problem (2), however, is more complicated. There doesn't appear to be a
TrimmedLength or any type of method or property that can give me the length
of a string, minus whitespace and without generating a new string instance,
in the BCL.
I suppose I could do some unsafe, or even unmanaged code (which is what MSFT
did for all their string handling stuff inside System.String and using the
COMString stuff), but I'd like to try to avoid that, or at least use a
library that's already written and well tested.
Any thoughts?
Thanks in advance,
Chad Myers
.
- Follow-Ups:
- Re: Fast string operations
- From: Jonathan Allen
- Re: Fast string operations
- From: Nicholas Paldino [.NET/C# MVP]
- Re: Fast string operations
- Prev by Date: Re: Word Editor
- Next by Date: Re: Re-number a column for all rows in RowChanged event?
- Previous by thread: Where does obselete attribute output?
- Next by thread: Re: Fast string operations
- Index(es):
Relevant Pages
|