Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / December 2005

Tip: Looking for answers? Try searching our database.

This code is efficient

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
fran - 19 Dec 2005 17:07 GMT
Hello, Is this code efficient?

public static string HTML_FASE1_OTROS_GENERAL =
"    <table id='tFase1' cellspacing='0' cellpadding='0' width='800' >" +
"        <tr>" +
"            <td width='15'></td>" +
"            <td width='750'><br>" +
............

Thanks
Kyong Kwak - 19 Dec 2005 17:13 GMT
> Hello, Is this code efficient?
>
[quoted text clipped - 5 lines]
> ............
> Thanks

no.. you'll want to use a string builder or

public static string HTML_FASE1_OTROS_GENERAL = @"    <table id='tFase1' cellspacing='0'
cellpadding='0' width='800' >
        <tr>
            <td width='15'></td>
            <td width='750'><br>" ;

something like that..  that way the you don't create multiple instances of
string.. and waste memory..
Nicholas Paldino [.NET/C# MVP] - 19 Dec 2005 17:32 GMT
Kyoung,

   But that's not the case here.  The compiler is actually going to
concatenate this at compile-time into one string.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

>> Hello, Is this code efficient?
>>
[quoted text clipped - 16 lines]
> something like that..  that way the you don't create multiple instances of
> string.. and waste memory..
tjb - 19 Dec 2005 17:37 GMT
>> Hello, Is this code efficient?
>>
[quoted text clipped - 3 lines]
>> "            <td width='15'></td>" +
>> "            <td width='750'><br>" +

<snip>

> something like that..  that way the you don't create multiple instances of
> string.. and waste memory..

No, the OP's code *doesn't* have this problem.  See
<http://www.pobox.com/~skeet/csharp/stringbuilder.html>.
Bruce Wood - 19 Dec 2005 18:17 GMT
On top of what the other posters have pointed out: that the compiler
will build a single string at compile time so there is no run-time
penalty, I should also point out that even if this weren't the case,
concatenating four or five strings like this is still going to be
cheaper than calls to StringBuilder, or the cost will be almost
identical. Personally, I wouldn't get bent out of shape over a few
string concats, especially in something that isn't inside a loop.
Kevin Spencer - 19 Dec 2005 19:15 GMT
> no.. you'll want to use a string builder or

Not correct. If the concatenation occurs in the same statement, multiple
string instances are not created.

Signature

HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.

>> Hello, Is this code efficient?
>>
[quoted text clipped - 16 lines]
> something like that..  that way the you don't create multiple instances of
> string.. and waste memory..
Nicholas Paldino [.NET/C# MVP] - 19 Dec 2005 17:16 GMT
Fran,

   Unless this changes a good deal, I would make it constant, if not, then
read-only, unless you have a reason you want other people to change it?

   You don't have to worry about the multiple strings.  The compiler will
reduce that to one string.

   Hope this helps.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> Hello, Is this code efficient?
>
[quoted text clipped - 6 lines]
>
> Thanks
Kevin Spencer - 19 Dec 2005 17:24 GMT
It is not inefficient.

Signature

HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.

> Hello, Is this code efficient?
>
[quoted text clipped - 6 lines]
>
> Thanks
Mark Rae - 19 Dec 2005 18:21 GMT
> It is not inefficient.

LOL!
fran - 19 Dec 2005 18:38 GMT
A similar question:

I would have to use replace of the String class or the one of the
StringBuilder class to replace 6 or 7 small substrings of a long string?

> Hello, Is this code efficient?
>
[quoted text clipped - 6 lines]
>
> Thanks
Bruce Wood - 19 Dec 2005 18:59 GMT
Keep in mind that every time you replace a substring in the long
string, you allocate a whole new long string and build the altered
string into it.

Given that, you have to ask yourself two questions:

1. How long is the "long" string? Personally, in this context, I
wouldn't pay much attention unless it's over a couple of hundred
characters, unless...

2. Are you doing this over and over again? In other words, are you
doing this in a loop? If so, then StringBuilder will probably make a
significant difference, unless...

3. Do you need the replacement to be case-insensitive or culturally
aware? StringBuilder's Replace replaces only the exact string you're
searching for, not any variants on case or culture. If you're building
an internationalized application then you may not be able to use the
StringBuilder version.

In all of this, the StringBuilder version won't be much more efficient
unless you set its Capacity property, or supply the capacity as an
"int" on the constructor, something like this:

StringBuilder sb = new StringBuilder(startingString,
startingString.Length * 2);

because if you don't leave ample space for the string to expand (if the
replacement strings are longer than what they're replacing) then
StringBuilder will just waste a bunch of time expanding itself over and
over to accommodate longer and longer strings, and each expansion is a
copy, just like String.Replace.

So, if you're doing this once when your program starts up, and the
string in question is 100 characters or something like that, don't
worry about it. I would just use
String.Replace().Replace().Replace()...

If you're doing this in a loop or the string is very long (1000
characters or more) then I'd use StringBuilder, but only if I were sure
that my application would never need to worry about international /
case concerns.
Bruce Wood - 19 Dec 2005 19:38 GMT
> Given that, you have to ask yourself *two* questions:

> 1.
> 2.
> 3.

Ah, yes. There are three kinds of people in the world: those who can do
math, and those who can't. I fit into the third group. :-)
Michael Bray - 19 Dec 2005 19:53 GMT
"Bruce Wood" <brucewood@canada.com> wrote in news:1135021132.786528.63550
@g49g2000cwa.googlegroups.com:

> Ah, yes. There are three kinds of people in the world: those who can do
> math, and those who can't. I fit into the third group. :-)

Actually there are 10 kinds of people.  Those who understand binary and
those who don't.  ;)

-mdb
Michael S - 20 Dec 2005 14:54 GMT
> "Bruce Wood" <brucewood@canada.com> wrote in news:1135021132.786528.63550
> @g49g2000cwa.googlegroups.com:
[quoted text clipped - 6 lines]
>
> -mdb

11. There are also the kind that think they understand binary, but doesn't
=)

- Michael S

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.