|
.net
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Question about stringCan 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. 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 -- Show quoteHide quoteCo-founder, Eggheadcafe.com developer portal: http://www.eggheadcafe.com UnBlog: http://petesbloggerama.blogspot.com "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. 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. Tests have shown that StringBuilder is more efficient on concatenation of at
least 7 or more parts. Peter -- Show quoteHide quoteCo-founder, Eggheadcafe.com developer portal: http://www.eggheadcafe.com UnBlog: http://petesbloggerama.blogspot.com "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. > > > Peter Bromberg [C# MVP] wrote:
> Tests have shown that StringBuilder is more efficient on You should really be using the StringBuilder for all string concatenation. > concatenation of at least 7 or more parts. > Peter > > 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. 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 -- Show quoteHide quoteCo-founder, Eggheadcafe.com developer portal: http://www.eggheadcafe.com UnBlog: http://petesbloggerama.blogspot.com "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. > > > > Peter Bromberg [C# MVP] wrote:
> Jim, Thanks, Peter. I'm aware of these and other such comments. Perhaps the scope > 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 > 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.
Other interesting topics
Help: ASP.Net broken (tried usual suspects...)
ASP.NET 2.0 menu: where to set alternate text for "^ up one level Error: 'CreateUser' is not a member of 'Membership' vs2005: domain trust relationship problem What is the database "standard" in 2.0? Question on string Need Step by Step guide for setting up IIS 6 for ASP DataGrid Columns Spannig More Than One Column XMLHTTP question asp.net cookie timeout |
|||||||||||||||||||||||