Re: Fast string operations
- From: "Chad Myers" <cmyers@xxxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 01 Jun 2005 20:43:37 GMT
Jonathon:
Thanks for your quick response.
Unfortunately, in (2), we're not doing that. In most cases, it's OK to have
padded strings, just not all-whitespace strings.
Regardless, even with your suggestion, the .Trim() still creates a new
string instance and fills the heap with crap :(
Thanks again,
Chad
"Jonathan Allen" <x@xxx> wrote in message
news:eXXXOfuZFHA.1512@xxxxxxxxxxxxxxxxxxxxxxx
>> 1.) if( someString.ToLower() == "somestring" )
>
> FxCop will actually catch and report instances of this for you. It is my
> 2nd favorite tool outside of Visual Studio.
>
>> 2.) if( someString != null && someString.Trim().Length > 0 )
>
> I would recommend using
>
> if (someString != null)
> someString = someString.Trim();
> else
> someString = "";
>
> if( someString.Length > 0 )
>
> My assumption here is that you already intend to trim the string before it
> is used.
>
> --
> Jonathan Allen
>
>
> "Chad Myers" <cmyers@xxxxxxxxxxxxxxxxxxxxx> wrote in message
> news:2rone.13780$PR6.9706@xxxxxxxxxxxxxxxxxxxxxxx
>> 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
>>
>>
>
>
.
- References:
- Fast string operations
- From: Chad Myers
- Re: Fast string operations
- From: Jonathan Allen
- Fast string operations
- Prev by Date: Re: Fast string operations
- Next by Date: Re: Fast string operations
- Previous by thread: Re: Fast string operations
- Next by thread: Re: Fast string operations
- Index(es):
Relevant Pages
|