Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Efficient String Concatenation

Efficient String Concatenation

Scheduled Pinned Locked Moved C#
questioncsharphelpcode-review
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Larry Antram
    wrote on last edited by
    #1

    I have a function that needs to accept a string and append it to another string. I initially implemented it like this:

    private String big = "";
    ...

    private void OnIncoming( String small )
    {
    big += small;
    big += "\r\n";
    }

    However this seems to be extremely inefficient. Receiving about 300K of text (which happens to come from the net) in small amounts takes about 1 minute. However, if I simply comment out the contents of OnIncoming (the two += lines) it takes about 5 seconds. So it appears the string concatenation is causing the problem. How can I improve this? I need to take a bunch of little strings (one at a time), and concatenate them into a larger string. Any suggestions as to what would be the most efficient way to do this in C#? Thanks in advance for any suggestions, ideas, or assistance!

    H 1 Reply Last reply
    0
    • L Larry Antram

      I have a function that needs to accept a string and append it to another string. I initially implemented it like this:

      private String big = "";
      ...

      private void OnIncoming( String small )
      {
      big += small;
      big += "\r\n";
      }

      However this seems to be extremely inefficient. Receiving about 300K of text (which happens to come from the net) in small amounts takes about 1 minute. However, if I simply comment out the contents of OnIncoming (the two += lines) it takes about 5 seconds. So it appears the string concatenation is causing the problem. How can I improve this? I need to take a bunch of little strings (one at a time), and concatenate them into a larger string. Any suggestions as to what would be the most efficient way to do this in C#? Thanks in advance for any suggestions, ideas, or assistance!

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      You should use String.Concat for up to 4 string parameters, String.Format if you can format your string, or - which can be used for all - a StringBuilder (which String.Format uses internally). Also, keep in mind that string are immutable while the StringBuilder is not (meaning it can actually grow). When you concatenate strings, the CLR must get the length of both strings, create a new string of the total length, then copy each character from both strings. You're right - it is very inefficient. The methods and class above will help you (and are nice for formatting and what-not, too).

      -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups