Re: Fast string operations

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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
>>
>>
>
>


.



Relevant Pages

  • Re: Deadlock in Solaris 8
    ... Are you using stl strings? ... operator created a new string instance from the constant data. ... which killing performance due to the multiprocessor environment. ...
    (comp.unix.solaris)
  • Re: Fast string operations
    ... the char[] for each string cached with the string, or is a new one created ... the string is full of whitespace. ... >> ToLowergenerates a new string instance as does Trim. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How to get the current allocated total size of an ArrayList?
    ... you do it one string at a time. ... allocate the ArrayList. ... want an array rather than an 'ArrayList'. ... PLUS the object header in the JVM for the String instance *and* the array instance. ...
    (comp.lang.java.help)
  • Re: System.String: It doesnt seem to act like a reference type.
    ... First, because Strings are immutable, changing the value of a string ... doesn't really change the string _in situ_ in memory. ... creates a whole new string and points the String reference to it. ... the new String instance instead of the old ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Fortran
    ... it didn't implement a working TRIM() in F77. ... CHARACTER*80 STRING ... EXTERNAL MYTRIM ... STRING = 'alongword' ...
    (comp.lang.fortran)