Home All Groups Group Topic Archive Search About
Author
12 Jan 2006 10:49 PM
Diffident
Dear Bruce,

Can you please explain me how compiler would be able to optimize "+" for the
below code?

The reason why I did not want to use string concatenation was that I wanted
it to be a single string object in an effort to reduce memory consumption.
But if I use "+" does not that consume more memory due to string's inherent
property of
immutability and heavy cost of concatenations?

Please correct me if I am wrong...

Show quoteHide quote
"Bruce Barker" wrote:

> use the "+" for readability, as the compiler will optimize it out.
>
> -- bruce (sqlwork.com)
>
>
>
> "Diffident" <Diffid***@discussions.microsoft.com> wrote in message
> news:D2B9A188-596C-4B5F-80AA-FA8D6A7E08A1@microsoft.com...
> > Hello All,
> >
> > I have a string of around 150 characters.
> >
> > string test = "asjhdkashdkjahsdjkhaskjdhaskjdhasjdhasd........."; //(upto
> > 150 characters)
> >
> > I want to split the above string into multiple lines in IDE while
> > declaring
> > it so that it is more readable. How can I do that?
> >
> > I do not want to concatenate using "+" nor do I want to use stringbuilder
> > object. I tried using @. If I use @ followed by the string, compiler is
> > inserting special characters like \t,\n. I think that I can StringBuilder
> > object but are there any other alternatives?
> >
> > My goal is to be able to declare string like this without using "+" or
> > stringbuilder spread accross in multiple lines in IDE.
> >
> > string test = "asjhdajkshdjkashdajksdhajskdhasd
> > sdfsjkdhfkjsdhfsjkdhfskjdfhskdjfhsd
> > sdfhsdjkfhsjdkfhsdjkfhsdjkfhsdkjfhs";
> >
> > I hope I made sense as to what I want to achieve....
> >
> > Thank you.

Author
12 Jan 2006 11:16 PM
Peter Bromberg [C# MVP]
Diffident,
If your string is only going to be up to 150 characters long, I think we are
focusing on the hole instead of the donut. You needn't be concerned about
memory consumption at all.
Hope that helps,
Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com




Show quoteHide quote
"Diffident" wrote:

> Dear Bruce,
>
> Can you please explain me how compiler would be able to optimize "+" for the
> below code?
>
> The reason why I did not want to use string concatenation was that I wanted
> it to be a single string object in an effort to reduce memory consumption.
> But if I use "+" does not that consume more memory due to string's inherent
> property of
> immutability and heavy cost of concatenations?
>
> Please correct me if I am wrong...
>
> "Bruce Barker" wrote:
>
> > use the "+" for readability, as the compiler will optimize it out.
> >
> > -- bruce (sqlwork.com)
> >
> >
> >
> > "Diffident" <Diffid***@discussions.microsoft.com> wrote in message
> > news:D2B9A188-596C-4B5F-80AA-FA8D6A7E08A1@microsoft.com...
> > > Hello All,
> > >
> > > I have a string of around 150 characters.
> > >
> > > string test = "asjhdkashdkjahsdjkhaskjdhaskjdhasjdhasd........."; //(upto
> > > 150 characters)
> > >
> > > I want to split the above string into multiple lines in IDE while
> > > declaring
> > > it so that it is more readable. How can I do that?
> > >
> > > I do not want to concatenate using "+" nor do I want to use stringbuilder
> > > object. I tried using @. If I use @ followed by the string, compiler is
> > > inserting special characters like \t,\n. I think that I can StringBuilder
> > > object but are there any other alternatives?
> > >
> > > My goal is to be able to declare string like this without using "+" or
> > > stringbuilder spread accross in multiple lines in IDE.
> > >
> > > string test = "asjhdajkshdjkashdajksdhajskdhasd
> > > sdfsjkdhfkjsdhfsjkdhfskjdfhskdjfhsd
> > > sdfhsdjkfhsjdkfhsdjkfhsdjkfhsdkjfhs";
> > >
> > > I hope I made sense as to what I want to achieve....
> > >
> > > Thank you.
Are all your drivers up to date? click for free checkup

Author
13 Jan 2006 2:03 AM
Daniel Walzenbach
Hi,

why don't you want to use stringbuilder?

Regards

   Daniel

Show quoteHide quote
"Diffident" <Diffid***@discussions.microsoft.com> schrieb im Newsbeitrag
news:B327FC09-403B-4264-8D2A-578D4E40B256@microsoft.com...
> Dear Bruce,
>
> Can you please explain me how compiler would be able to optimize "+" for
> the
> below code?
>
> The reason why I did not want to use string concatenation was that I
> wanted
> it to be a single string object in an effort to reduce memory consumption.
> But if I use "+" does not that consume more memory due to string's
> inherent
> property of
> immutability and heavy cost of concatenations?
>
> Please correct me if I am wrong...
>
> "Bruce Barker" wrote:
>
>> use the "+" for readability, as the compiler will optimize it out.
>>
>> -- bruce (sqlwork.com)
>>
>>
>>
>> "Diffident" <Diffid***@discussions.microsoft.com> wrote in message
>> news:D2B9A188-596C-4B5F-80AA-FA8D6A7E08A1@microsoft.com...
>> > Hello All,
>> >
>> > I have a string of around 150 characters.
>> >
>> > string test = "asjhdkashdkjahsdjkhaskjdhaskjdhasjdhasd.........";
>> > //(upto
>> > 150 characters)
>> >
>> > I want to split the above string into multiple lines in IDE while
>> > declaring
>> > it so that it is more readable. How can I do that?
>> >
>> > I do not want to concatenate using "+" nor do I want to use
>> > stringbuilder
>> > object. I tried using @. If I use @ followed by the string, compiler is
>> > inserting special characters like \t,\n. I think that I can
>> > StringBuilder
>> > object but are there any other alternatives?
>> >
>> > My goal is to be able to declare string like this without using "+" or
>> > stringbuilder spread accross in multiple lines in IDE.
>> >
>> > string test = "asjhdajkshdjkashdajksdhajskdhasd
>> > sdfsjkdhfkjsdhfsjkdhfskjdfhskdjfhsd
>> > sdfhsdjkfhsjdkfhsdjkfhsdjkfhsdkjfhs";
>> >
>> > I hope I made sense as to what I want to achieve....
>> >
>> > Thank you.
Author
13 Jan 2006 11:52 AM
Peter Bromberg [C# MVP]
Tests have shown that StringBuilder is more efficient on concatenation of at
least 7 or more parts.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com




Show quoteHide quote
"Daniel Walzenbach" wrote:

> Hi,
>
> why don't you want to use stringbuilder?
>
> Regards
>
>    Daniel
>
> "Diffident" <Diffid***@discussions.microsoft.com> schrieb im Newsbeitrag
> news:B327FC09-403B-4264-8D2A-578D4E40B256@microsoft.com...
> > Dear Bruce,
> >
> > Can you please explain me how compiler would be able to optimize "+" for
> > the
> > below code?
> >
> > The reason why I did not want to use string concatenation was that I
> > wanted
> > it to be a single string object in an effort to reduce memory consumption.
> > But if I use "+" does not that consume more memory due to string's
> > inherent
> > property of
> > immutability and heavy cost of concatenations?
> >
> > Please correct me if I am wrong...
> >
> > "Bruce Barker" wrote:
> >
> >> use the "+" for readability, as the compiler will optimize it out.
> >>
> >> -- bruce (sqlwork.com)
> >>
> >>
> >>
> >> "Diffident" <Diffid***@discussions.microsoft.com> wrote in message
> >> news:D2B9A188-596C-4B5F-80AA-FA8D6A7E08A1@microsoft.com...
> >> > Hello All,
> >> >
> >> > I have a string of around 150 characters.
> >> >
> >> > string test = "asjhdkashdkjahsdjkhaskjdhaskjdhasjdhasd.........";
> >> > //(upto
> >> > 150 characters)
> >> >
> >> > I want to split the above string into multiple lines in IDE while
> >> > declaring
> >> > it so that it is more readable. How can I do that?
> >> >
> >> > I do not want to concatenate using "+" nor do I want to use
> >> > stringbuilder
> >> > object. I tried using @. If I use @ followed by the string, compiler is
> >> > inserting special characters like \t,\n. I think that I can
> >> > StringBuilder
> >> > object but are there any other alternatives?
> >> >
> >> > My goal is to be able to declare string like this without using "+" or
> >> > stringbuilder spread accross in multiple lines in IDE.
> >> >
> >> > string test = "asjhdajkshdjkashdajksdhajskdhasd
> >> > sdfsjkdhfkjsdhfsjkdhfskjdfhskdjfhsd
> >> > sdfhsdjkfhsjdkfhsdjkfhsdjkfhsdkjfhs";
> >> >
> >> > I hope I made sense as to what I want to achieve....
> >> >
> >> > Thank you.
>
>
>
Author
13 Jan 2006 1:41 PM
Jim Cheshire
Peter Bromberg [C# MVP] wrote:
> Tests have shown that StringBuilder is more efficient on
> concatenation of at least 7 or more parts.
> Peter
>
>

You should really be using the StringBuilder for all string concatenation.
If your site is under load, you can see a substantial increase in memory due
to strings even when concatenating few strings. We see this issue pretty
often.

--
Jim Cheshire
================================
Blog: http://blogs.msdn.com/jamesche

Latest entry:
Getting the PID and TID of a COM Call

Describes how to get the PID of the
dllhost process a COM call is executing
in and how to locate the thread as well.
Author
13 Jan 2006 2:34 PM
Peter Bromberg [C# MVP]
Jim,
I would be reluctant to accept a blanket statement such as this.
Stringbuilder is *NOT* always faster / more efficient for string
concatenation.

Read two resources (there are others):

http://geekswithblogs.net/johnsperfblog/archive/2005/05/27.aspx

http://www.heikniemi.net/hc/archives/000124.html

Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com




Show quoteHide quote
"Jim Cheshire" wrote:

> Peter Bromberg [C# MVP] wrote:
> > Tests have shown that StringBuilder is more efficient on
> > concatenation of at least 7 or more parts.
> > Peter
> >
> >
>
> You should really be using the StringBuilder for all string concatenation.
> If your site is under load, you can see a substantial increase in memory due
> to strings even when concatenating few strings. We see this issue pretty
> often.
>
> --
> Jim Cheshire
> ================================
> Blog: http://blogs.msdn.com/jamesche
>
> Latest entry:
> Getting the PID and TID of a COM Call
>
> Describes how to get the PID of the
> dllhost process a COM call is executing
> in and how to locate the thread as well.
>
>
>
>
Author
13 Jan 2006 7:22 PM
Jim Cheshire
Peter Bromberg [C# MVP] wrote:
> Jim,
> I would be reluctant to accept a blanket statement such as this.
> Stringbuilder is *NOT* always faster / more efficient for string
> concatenation.
>
> Read two resources (there are others):
>
> http://geekswithblogs.net/johnsperfblog/archive/2005/05/27.aspx
>
> http://www.heikniemi.net/hc/archives/000124.html
>

Thanks, Peter. I'm aware of these and other such comments. Perhaps the scope
of my statement was too broad. I should have said that you should *almost*
always use SB, but note that I'm not talking about performance here. I'm
talking about the large number of enterprise applications that I have seen
that struggle with memory issues because of reading articles just such as
you've outlined here.

--
Jim Cheshire
================================
Blog: http://blogs.msdn.com/jamesche

Latest entry:
Getting the PID and TID of a COM Call

Describes how to get the PID of the
dllhost process a COM call is executing
in and how to locate the thread as well.

Bookmark and Share