String.Concat vs String.Format

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

    String.Concat vs String.Format

    I know its a sin to use strings, lets skip that part...

    Which of these is faster and uses less memory?

    String.Format(" SomeValue='{0}' ", m_Value);

    or

    String.Concat(" SomeValue='", m_Value, "'");

    - ramadu
  • cody

    #2
    Re: String.Concat vs String.Format

    If you think a bit about it you 'll get the answer.

    String.Concat is made to do exactly this where it get the name from, whereas
    String.Format can do lots of stuff and has to scan the string for format
    specifiers first.


    "ramadu" <tnr@newsgroups .nospam> schrieb im Newsbeitrag
    news:%23RO%23dL AjGHA.1324@TK2M SFTNGP04.phx.gb l...[color=blue]
    >I know its a sin to use strings, lets skip that part...
    >
    > Which of these is faster and uses less memory?
    >
    > String.Format(" SomeValue='{0}' ", m_Value);
    >
    > or
    >
    > String.Concat(" SomeValue='", m_Value, "'");
    >
    > - ramadu[/color]


    Comment

    • ramadu

      #3
      Re: String.Concat vs String.Format

      I realize that but does .net do any compiler optimizations if
      string.Format is used?

      - ramadu

      :[color=blue]
      > If you think a bit about it you 'll get the answer.
      >
      > String.Concat is made to do exactly this where it get the name from, whereas
      > String.Format can do lots of stuff and has to scan the string for format
      > specifiers first.
      >
      >
      > "ramadu" <tnr@newsgroups .nospam> schrieb im Newsbeitrag
      > news:%23RO%23dL AjGHA.1324@TK2M SFTNGP04.phx.gb l...[color=green]
      >> I know its a sin to use strings, lets skip that part...
      >>
      >> Which of these is faster and uses less memory?
      >>
      >> String.Format(" SomeValue='{0}' ", m_Value);
      >>
      >> or
      >>
      >> String.Concat(" SomeValue='", m_Value, "'");
      >>
      >> - ramadu[/color]
      >
      >[/color]

      Comment

      • Michael Nemtsev

        #4
        Re: String.Concat vs String.Format

        Hello ramadu,

        If u use reflector you can see that String.Format use StringBuilder class
        and calls its AppendFormat method that is pretty rich.
        String,Contat uses string.FastAllo cateString and FillStringCheck ed methods

        I'd assume that String.Concat is more fastest case

        r> I know its a sin to use strings, lets skip that part...
        r>
        r> Which of these is faster and uses less memory?
        r>
        r> String.Format(" SomeValue='{0}' ", m_Value);
        r>
        r> or
        r>
        r> String.Concat(" SomeValue='", m_Value, "'");
        r>
        r> - ramadu
        r>
        ---
        WBR,
        Michael Nemtsev :: blog: http://spaces.msn.com/laflour

        "At times one remains faithful to a cause only because its opponents do not
        cease to be insipid." (c) Friedrich Nietzsche


        Comment

        • ramadu

          #5
          Re: String.Concat vs String.Format

          But in that case String.Concat will be costly in terms of memory as it
          will create multiple immutable strings...

          - ramadu

          :[color=blue]
          > Hello ramadu,
          >
          > If u use reflector you can see that String.Format use StringBuilder
          > class and calls its AppendFormat method that is pretty rich.
          > String,Contat uses string.FastAllo cateString and FillStringCheck ed methods
          >
          > I'd assume that String.Concat is more fastest case
          >
          > r> I know its a sin to use strings, lets skip that part...
          > r> r> Which of these is faster and uses less memory?
          > r> r> String.Format(" SomeValue='{0}' ", m_Value);
          > r> r> or
          > r> r> String.Concat(" SomeValue='", m_Value, "'");
          > r> r> - ramadu
          > r> ---
          > WBR,
          > Michael Nemtsev :: blog: http://spaces.msn.com/laflour
          >
          > "At times one remains faithful to a cause only because its opponents do
          > not cease to be insipid." (c) Friedrich Nietzsche
          >
          >[/color]

          Comment

          • chanmm

            #6
            Re: String.Concat vs String.Format

            Just for the sake of discussion. All your variables has a scope. How many
            variable can you accumulate till you quit the program? So, to me I would say
            I use whichever way I feel is comfortable to me.

            chanmm

            "ramadu" <tnr@newsgroups .nospam> wrote in message
            news:%23RO%23dL AjGHA.1324@TK2M SFTNGP04.phx.gb l...[color=blue]
            >I know its a sin to use strings, lets skip that part...
            >
            > Which of these is faster and uses less memory?
            >
            > String.Format(" SomeValue='{0}' ", m_Value);
            >
            > or
            >
            > String.Concat(" SomeValue='", m_Value, "'");
            >
            > - ramadu[/color]


            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: String.Concat vs String.Format

              ramadu <tnr@newsgroups .nospam> wrote:[color=blue]
              > I know its a sin to use strings, lets skip that part...[/color]

              In what way? Using strings is part of every day life. Is there some
              particular use you're thinking of which isn't a good idea, like using
              values directly in SQL statements?
              [color=blue]
              > Which of these is faster and uses less memory?
              >
              > String.Format(" SomeValue='{0}' ", m_Value);
              >
              > or
              >
              > String.Concat(" SomeValue='", m_Value, "'");[/color]

              Well, the Concat call is faster, *but* it's unlikely to ever be
              significant. The more important question is "Which of these is clearer,
              and more readable?" There, the answer is String.Format IMO. On the
              other hand, your Concat call could be made easier to read too:

              string x = "SomeValue= '" + m_Value + "'";

              That will compile to the same code, and is fairly readable. I'd still
              use String.Format though, I think.

              --
              Jon Skeet - <skeet@pobox.co m>
              http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
              If replying to the group, please do not mail me too

              Comment

              • Mark Wilden

                #8
                Re: String.Concat vs String.Format

                "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
                news:MPG.1ef473 1d33dbc43798d22 d@msnews.micros oft.com...
                [color=blue][color=green]
                >> Which of these is faster and uses less memory?
                >>
                >> String.Format(" SomeValue='{0}' ", m_Value);
                >>
                >> or
                >>
                >> String.Concat(" SomeValue='", m_Value, "'");[/color]
                >
                > Well, the Concat call is faster, *but* it's unlikely to ever be
                > significant. The more important question is "Which of these is clearer,
                > and more readable?" There, the answer is String.Format IMO.[/color]

                I don't know -- when you're just concatenating strings, + (or Concat) is
                more readable, IMO.

                ///ark


                Comment

                • cody

                  #9
                  Re: String.Concat vs String.Format

                  what do you mean with "compiler optimizations"?

                  "ramadu" <tnr@newsgroups .nospam> schrieb im Newsbeitrag
                  news:4489DF26.3 040400@newsgrou ps.nospam...[color=blue]
                  >I realize that but does .net do any compiler optimizations if string.Format
                  >is used?
                  >
                  > - ramadu
                  >
                  > :[color=green]
                  >> If you think a bit about it you 'll get the answer.
                  >>
                  >> String.Concat is made to do exactly this where it get the name from,
                  >> whereas String.Format can do lots of stuff and has to scan the string for
                  >> format specifiers first.
                  >>
                  >>
                  >> "ramadu" <tnr@newsgroups .nospam> schrieb im Newsbeitrag
                  >> news:%23RO%23dL AjGHA.1324@TK2M SFTNGP04.phx.gb l...[color=darkred]
                  >>> I know its a sin to use strings, lets skip that part...
                  >>>
                  >>> Which of these is faster and uses less memory?
                  >>>
                  >>> String.Format(" SomeValue='{0}' ", m_Value);
                  >>>
                  >>> or
                  >>>
                  >>> String.Concat(" SomeValue='", m_Value, "'");
                  >>>
                  >>> - ramadu[/color]
                  >>[/color][/color]

                  Comment

                  • Göran Andersson

                    #10
                    Re: String.Concat vs String.Format

                    The Concat would be slightly faster and memory efficient, but if you are
                    trying to optimize your code you are probably looking in the wrong place.

                    What are you going to use the string for?

                    ramadu wrote:[color=blue]
                    > I know its a sin to use strings, lets skip that part...
                    >
                    > Which of these is faster and uses less memory?
                    >
                    > String.Format(" SomeValue='{0}' ", m_Value);
                    >
                    > or
                    >
                    > String.Concat(" SomeValue='", m_Value, "'");
                    >
                    > - ramadu[/color]

                    Comment

                    • JimD

                      #11
                      Re: String.Concat vs String.Format

                      Mark Wilden wrote:[color=blue]
                      > "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
                      > news:MPG.1ef473 1d33dbc43798d22 d@msnews.micros oft.com...
                      >[color=green][color=darkred]
                      >>> Which of these is faster and uses less memory?
                      >>>
                      >>> String.Format(" SomeValue='{0}' ", m_Value);
                      >>>
                      >>> or
                      >>>
                      >>> String.Concat(" SomeValue='", m_Value, "'");[/color]
                      >> Well, the Concat call is faster, *but* it's unlikely to ever be
                      >> significant. The more important question is "Which of these is clearer,
                      >> and more readable?" There, the answer is String.Format IMO.[/color]
                      >
                      > I don't know -- when you're just concatenating strings, + (or Concat) is
                      > more readable, IMO.
                      >
                      > ///ark[/color]

                      I think it depends on what you are doing:

                      version #1:

                      string sql = "";
                      sql = "Insert Into Foo (custid, salary, bar, snoopy, bugs)"
                      + " values (" + m_custid + ", " + m_sal + ", '" + m_bar + "', '"
                      + m_snoopy + "', '" + m_bugs + "')";



                      version #2

                      // this string would probably be defined somewhere else
                      string sqlIns = Insert Into Foo (custid, salary, bar, snoopy, bugs)"
                      + " values ({0}, {1}, '{2}', '{3}', '{4}')";

                      string sql = String.Format(s qlIns, m_custid, m_sal,
                      m_bar, m_snoopy, m_bugs);

                      I think version #2 is the cleanest.

                      Jim
                      --
                      =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
                      You roll an 18 in Dex and see if you
                      don't end up with a girlfriend
                      =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
                      JimD
                      Central FL, USA, Earth, Sol

                      Comment

                      • Mark Wilden

                        #12
                        Re: String.Concat vs String.Format

                        "JimD" <Jim@keeliegirl .dyndns.org> wrote in message
                        news:JiMig.1022 7$Ui7.9606@torn ado.tampabay.rr .com...
                        [color=blue]
                        > // this string would probably be defined somewhere else
                        > string sqlIns = Insert Into Foo (custid, salary, bar, snoopy, bugs)"
                        > + " values ({0}, {1}, '{2}', '{3}', '{4}')";
                        >
                        > string sql = String.Format(s qlIns, m_custid, m_sal,
                        > m_bar, m_snoopy, m_bugs);
                        >
                        > I think version #2 is the cleanest.[/color]

                        I agree, but that isn't concatenation.

                        ///ark


                        Comment

                        • Jon Skeet [C# MVP]

                          #13
                          Re: String.Concat vs String.Format

                          Mark Wilden <MarkWilden@new sgroups.nospam> wrote:[color=blue]
                          > "JimD" <Jim@keeliegirl .dyndns.org> wrote in message
                          > news:JiMig.1022 7$Ui7.9606@torn ado.tampabay.rr .com...
                          >[color=green]
                          > > // this string would probably be defined somewhere else
                          > > string sqlIns = Insert Into Foo (custid, salary, bar, snoopy, bugs)"
                          > > + " values ({0}, {1}, '{2}', '{3}', '{4}')";
                          > >
                          > > string sql = String.Format(s qlIns, m_custid, m_sal,
                          > > m_bar, m_snoopy, m_bugs);
                          > >
                          > > I think version #2 is the cleanest.[/color]
                          >
                          > I agree, but that isn't concatenation.[/color]

                          Version 1 was though. You said:

                          "I don't know -- when you're just concatenating strings, + (or Concat)
                          is more readable, IMO."

                          Tha alternative to the Format call above was:

                          string sql = "";
                          sql = "Insert Into Foo (custid, salary, bar, snoopy, bugs)"
                          + " values (" + m_custid + ", " + m_sal + ", '" + m_bar + "', '"
                          + m_snoopy + "', '" + m_bugs + "')";

                          That's just concatenating strings, which would suggest that you'd
                          prefer that to the Format version.

                          --
                          Jon Skeet - <skeet@pobox.co m>
                          http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                          If replying to the group, please do not mail me too

                          Comment

                          • Mark Wilden

                            #14
                            Re: String.Concat vs String.Format

                            "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
                            news:MPG.1ef5bc 5a1578ed5698d23 3@msnews.micros oft.com...
                            [color=blue]
                            > string sql = "";
                            > sql = "Insert Into Foo (custid, salary, bar, snoopy, bugs)"
                            > + " values (" + m_custid + ", " + m_sal + ", '" + m_bar + "', '"
                            > + m_snoopy + "', '" + m_bugs + "')";
                            >
                            > That's just concatenating strings[/color]

                            Yes, you're right.

                            When pasting two strings together, I think + is simplest and clearest. When
                            formatting a long mixture of string constants and string variables, I think
                            Format is simpler. People talk about the former as concatenation and the
                            latter as formatting.

                            However, it's just my personal preference; I don't think it makes too much
                            difference either way. I certainly wouldn't worry about Format taking a
                            couple more ticks to execute.

                            (BTW, as long as we're on the subject, your StringBuilder page has let me
                            astound and amaze more than one of my colleagues who shun + like vampires.)


                            Comment

                            • ramadu

                              #15
                              Re: String.Concat vs String.Format

                              Its mostly for select statements in a DataTable.

                              - Sri

                              :[color=blue]
                              > The Concat would be slightly faster and memory efficient, but if you are
                              > trying to optimize your code you are probably looking in the wrong place.
                              >
                              > What are you going to use the string for?
                              >
                              > ramadu wrote:[color=green]
                              >> I know its a sin to use strings, lets skip that part...
                              >>
                              >> Which of these is faster and uses less memory?
                              >>
                              >> String.Format(" SomeValue='{0}' ", m_Value);
                              >>
                              >> or
                              >>
                              >> String.Concat(" SomeValue='", m_Value, "'");
                              >>
                              >> - ramadu[/color][/color]

                              Comment

                              Working...