This code is efficient

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • fran

    #1

    This code is efficient

    Hello, Is this code efficient?

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

    Thanks

  • Kyong Kwak

    #2
    Re: This code is efficient

    > Hello, Is this code efficient?[color=blue]
    >
    > public static string HTML_FASE1_OTRO S_GENERAL =
    > " <table id='tFase1' cellspacing='0' cellpadding='0' width='800' >" +
    > " <tr>" +
    > " <td width='15'></td>" +
    > " <td width='750'><br >" +
    > ............
    > Thanks
    >[/color]

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

    public static string HTML_FASE1_OTRO S_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..


    Comment

    • Nicholas Paldino [.NET/C# MVP]

      #3
      Re: This code is efficient

      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]
      - mvp@spam.guard. caspershouse.co m

      "fran" <fran@discussio ns.microsoft.co m> wrote in message
      news:E2024B23-A378-4D20-9E08-CCB66F94BDED@mi crosoft.com...[color=blue]
      > Hello, Is this code efficient?
      >
      > public static string HTML_FASE1_OTRO S_GENERAL =
      > " <table id='tFase1' cellspacing='0' cellpadding='0' width='800' >" +
      > " <tr>" +
      > " <td width='15'></td>" +
      > " <td width='750'><br >" +
      > ............
      >
      > Thanks
      >[/color]


      Comment

      • Kevin Spencer

        #4
        Re: This code is efficient

        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" <fran@discussio ns.microsoft.co m> wrote in message
        news:E2024B23-A378-4D20-9E08-CCB66F94BDED@mi crosoft.com...[color=blue]
        > Hello, Is this code efficient?
        >
        > public static string HTML_FASE1_OTRO S_GENERAL =
        > " <table id='tFase1' cellspacing='0' cellpadding='0' width='800' >" +
        > " <tr>" +
        > " <td width='15'></td>" +
        > " <td width='750'><br >" +
        > ............
        >
        > Thanks
        >[/color]


        Comment

        • Nicholas Paldino [.NET/C# MVP]

          #5
          Re: This code is efficient

          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]
          - mvp@spam.guard. caspershouse.co m

          "Kyong Kwak" <kyongkwak@nosp am.nospam> wrote in message
          news:7bf54e6511 1448c7d293fcdf9 1ea@news.micros oft.com...[color=blue][color=green]
          >> Hello, Is this code efficient?
          >>
          >> public static string HTML_FASE1_OTRO S_GENERAL =
          >> " <table id='tFase1' cellspacing='0' cellpadding='0' width='800' >" +
          >> " <tr>" +
          >> " <td width='15'></td>" +
          >> " <td width='750'><br >" +
          >> ............
          >> Thanks
          >>[/color]
          >
          > no.. you'll want to use a string builder or
          >
          > public static string HTML_FASE1_OTRO S_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..
          >
          >[/color]


          Comment

          • tjb

            #6
            Re: This code is efficient

            Kyong Kwak <kyongkwak@nosp am.nospam> wrote:
            [color=blue][color=green]
            >> Hello, Is this code efficient?
            >>
            >> public static string HTML_FASE1_OTRO S_GENERAL =
            >> " <table id='tFase1' cellspacing='0' cellpadding='0' width='800' >" +
            >> " <tr>" +
            >> " <td width='15'></td>" +
            >> " <td width='750'><br >" +[/color][/color]

            <snip>
            [color=blue]
            > something like that.. that way the you don't create multiple instances of
            > string.. and waste memory..[/color]

            No, the OP's code *doesn't* have this problem. See
            <http://www.pobox.com/~skeet/csharp/stringbuilder.h tml>.

            Comment

            • Bruce Wood

              #7
              Re: This code is efficient

              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.

              Comment

              • Mark Rae

                #8
                Re: This code is efficient

                "Kevin Spencer" <kevin@DIESPAMM ERSDIEtakempis. com> wrote in message
                news:%23k79iEMB GHA.2920@tk2msf tngp13.phx.gbl. ..
                [color=blue]
                > It is not inefficient.[/color]

                LOL!


                Comment

                • fran

                  #9
                  RE: This code is efficient

                  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:
                  [color=blue]
                  > Hello, Is this code efficient?
                  >
                  > public static string HTML_FASE1_OTRO S_GENERAL =
                  > " <table id='tFase1' cellspacing='0' cellpadding='0' width='800' >" +
                  > " <tr>" +
                  > " <td width='15'></td>" +
                  > " <td width='750'><br >" +
                  > ............
                  >
                  > Thanks
                  >[/color]

                  Comment

                  • Bruce Wood

                    #10
                    Re: This code is efficient

                    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 internationaliz ed 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(s tartingString,
                    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().Rep lace()...

                    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.

                    Comment

                    • Kevin Spencer

                      #11
                      Re: This code is efficient

                      > 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" <kyongkwak@nosp am.nospam> wrote in message
                      news:7bf54e6511 1448c7d293fcdf9 1ea@news.micros oft.com...[color=blue][color=green]
                      >> Hello, Is this code efficient?
                      >>
                      >> public static string HTML_FASE1_OTRO S_GENERAL =
                      >> " <table id='tFase1' cellspacing='0' cellpadding='0' width='800' >" +
                      >> " <tr>" +
                      >> " <td width='15'></td>" +
                      >> " <td width='750'><br >" +
                      >> ............
                      >> Thanks
                      >>[/color]
                      >
                      > no.. you'll want to use a string builder or
                      >
                      > public static string HTML_FASE1_OTRO S_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..
                      >
                      >[/color]


                      Comment

                      • Bruce Wood

                        #12
                        Re: This code is efficient

                        > Given that, you have to ask yourself *two* questions:
                        [color=blue]
                        > 1.
                        > 2.
                        > 3.[/color]

                        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. :-)

                        Comment

                        • Michael Bray

                          #13
                          Re: This code is efficient

                          "Bruce Wood" <brucewood@cana da.com> wrote in news:1135021132 .786528.63550
                          @g49g2000cwa.go oglegroups.com:
                          [color=blue]
                          > 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. :-)
                          >[/color]

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

                          -mdb

                          Comment

                          • Michael S

                            #14
                            Re: This code is efficient


                            "Michael Bray" <mbray@makeDInt oDot_ctiusaDcom > wrote in message
                            news:Xns9731978 0F4142mbrayctiu sacom@207.46.24 8.16...[color=blue]
                            > "Bruce Wood" <brucewood@cana da.com> wrote in news:1135021132 .786528.63550
                            > @g49g2000cwa.go oglegroups.com:
                            >[color=green]
                            >> 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. :-)
                            >>[/color]
                            >
                            > Actually there are 10 kinds of people. Those who understand binary and
                            > those who don't. ;)
                            >
                            > -mdb[/color]

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

                            - Michael S


                            Comment

                            Working...