471,350 Members | 1,943 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

This code is efficient

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

Dec 19 '05 #1
13 1408
> 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..
Dec 19 '05 #2
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"fran" <fr**@discussions.microsoft.com> wrote in message
news:E2**********************************@microsof t.com...
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

Dec 19 '05 #3
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.

"fran" <fr**@discussions.microsoft.com> wrote in message
news:E2**********************************@microsof t.com...
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

Dec 19 '05 #4
Kyoung,

But that's not the case here. The compiler is actually going to
concatenate this at compile-time into one string.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Kyong Kwak" <ky*******@nospam.nospam> wrote in message
news:7b**************************@news.microsoft.c om...
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..

Dec 19 '05 #5
tjb
Kyong Kwak <ky*******@nospam.nospam> wrote:
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>" +

<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>.
Dec 19 '05 #6
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.

Dec 19 '05 #7
"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
It is not inefficient.


LOL!
Dec 19 '05 #8
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?

"fran" wrote:
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

Dec 19 '05 #9
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.

Dec 19 '05 #10
> 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.

"Kyong Kwak" <ky*******@nospam.nospam> wrote in message
news:7b**************************@news.microsoft.c om...
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..

Dec 19 '05 #11
> 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. :-)

Dec 19 '05 #12
"Bruce Wood" <br*******@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
Dec 19 '05 #13

"Michael Bray" <mbray@makeDIntoDot_ctiusaDcom> wrote in message
news:Xn****************************@207.46.248.16. ..
"Bruce Wood" <br*******@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


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

- Michael S
Dec 20 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by christopher diggins | last post: by
12 posts views Thread by Michael Culley | last post: by
83 posts views Thread by deppy_3 | last post: by
20 posts views Thread by laredotornado | last post: by
12 posts views Thread by susiedba | last post: by
25 posts views Thread by Abubakar | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.