This code is efficient

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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

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..
 
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.
 
It is not inefficient.

--
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.
 
Kyoung,

But that's not the case here. The compiler is actually going to
concatenate this at compile-time into one string.
 
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.
 
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?
 
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.
 
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.

--
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.
 
Given that, you have to ask yourself *two* questions:

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. :-)
 
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 Bray said:
Actually there are 10 kinds of people. Those who understand binary and
those who don't. ;)

-mdb

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

- Michael S
 
Back
Top