Performance problems of StreamWriter

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ºa¤Ö

    #1

    Performance problems of StreamWriter

    Dear All,

    Which performance is better? I think 2 is better because only 1 I/O is
    performed. Am I right?

    1.
    using (StreamWriter sw = File.Append(Fil eName))
    {
    sw.WriteLine(te xt1);
    sw.WriteLine(te xt2);
    sw.WriteLine(te xt3);
    sw.WriteLine(te xt4);
    sw.WriteLine(te xt5);
    }

    2.
    using (StreamWriter sw = File.Append(Fil eName))
    {
    StringBuilder sb = new StringBuilder() ;
    sb.Append(text1 );
    sb.Append(text2 );
    sb.Append(text3 );
    sb.Append(text4 );
    sb.Append(text5 );

    sw.WriteLine (sb.ToString()) ;
    }


  • Peter Duniho

    #2
    Re: Performance problems of StreamWriter

    In article <#udhOGEGIHA.36 00@TK2MSFTNGP06 .phx.gbl>
    "ºa¤Ö"<ºa¤Ö@¤W« ˤ½¥q.­»´äwrote :
    Dear All,
    Which performance is better? I think 2 is better because only 1 I/O
    is performed. Am I right?
    Well, for one, the two pieces of code don't do the same thing. You
    left out the NewLine character in the second version.

    As far as whether one performs better than the other, I don't know.
    You'd have to look carefully at the implementation of the underlying
    stream. If it does some buffering, or if it handles the NewLine
    characters in some special way when given a complete string with
    multiple NewLine characters, or some other factor, it could be that
    individual calls to WriteLine are either no slower than a single one
    preceded by assembling a single string with a StringBuilder, or they
    could even be faster in some theoretical world.

    Personally, seems like a waste of time to think about it, at least
    without having some specific reason to. Which one is more
    maintainable in the context in which you're doing the operation? I
    doubt there's a significant performance difference either way and
    absent any real-world evidence that there is, you should instead focus
    on making the code _work_, and be maintainable, and not worry so much
    about some subtle performance difference.

    Pete

    --
    I'm trying a new usenet client for Mac, Nemo OS X.
    You can download it at http://www.malcom-mac.com/nemo

    Comment

    • Andrew Faust

      #3
      Re: Performance problems of StreamWriter

      Why wonder? Just build it and try it out.
      --
      Andrew Faust
      andrew[at]andrewfaust.com



      "ºa¤Ö" <ºa¤Ö@¤W«Ë¤½¥q. ­»´äwrote in message
      news:%23udhOGEG IHA.3600@TK2MSF TNGP06.phx.gbl. ..
      Dear All,
      >
      Which performance is better? I think 2 is better because only 1 I/O is
      performed. Am I right?
      >
      1.
      using (StreamWriter sw = File.Append(Fil eName))
      {
      sw.WriteLine(te xt1);
      sw.WriteLine(te xt2);
      sw.WriteLine(te xt3);
      sw.WriteLine(te xt4);
      sw.WriteLine(te xt5);
      }
      >
      2.
      using (StreamWriter sw = File.Append(Fil eName))
      {
      StringBuilder sb = new StringBuilder() ;
      sb.Append(text1 );
      sb.Append(text2 );
      sb.Append(text3 );
      sb.Append(text4 );
      sb.Append(text5 );
      >
      sw.WriteLine (sb.ToString()) ;
      }
      >

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Performance problems of StreamWriter

        ºa¤Ö <ºa¤Ö@¤W«Ë¤½¥q. ­»´äwrote:
        Which performance is better? I think 2 is better because only 1 I/O is
        performed. Am I right?

        1.
        using (StreamWriter sw = File.Append(Fil eName))
        {
        sw.WriteLine(te xt1);
        sw.WriteLine(te xt2);
        sw.WriteLine(te xt3);
        sw.WriteLine(te xt4);
        sw.WriteLine(te xt5);
        }

        2.
        using (StreamWriter sw = File.Append(Fil eName))
        {
        StringBuilder sb = new StringBuilder() ;
        sb.Append(text1 );
        sb.Append(text2 );
        sb.Append(text3 );
        sb.Append(text4 );
        sb.Append(text5 );

        sw.WriteLine (sb.ToString()) ;
        }
        Have you measured it? Do you *actually* have a problem, or are you just
        guessing that you might?

        I believe StreamWriter does buffering anyway, but either way I would
        use the most straightforward code until I'd seen an actual performance
        issue.

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

        • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

          #5
          Re: Performance problems of StreamWriter

          ºa¤Ö wrote:
          Which performance is better? I think 2 is better because only 1 I/O is
          performed. Am I right?
          >
          1.
          using (StreamWriter sw = File.Append(Fil eName))
          {
          sw.WriteLine(te xt1);
          sw.WriteLine(te xt2);
          sw.WriteLine(te xt3);
          sw.WriteLine(te xt4);
          sw.WriteLine(te xt5);
          }
          >
          2.
          using (StreamWriter sw = File.Append(Fil eName))
          {
          StringBuilder sb = new StringBuilder() ;
          sb.Append(text1 );
          sb.Append(text2 );
          sb.Append(text3 );
          sb.Append(text4 );
          sb.Append(text5 );
          >
          sw.WriteLine (sb.ToString()) ;
          }
          #2 is a good example of performance tuning when it is worst.

          Difficult to read code (you forgot the newlines yourself - what do you
          think the maintenance programmer will do?).

          Code that actually *decreases* performance.

          Stick with #1.

          And if you need to optimize it for writing large files, then
          use the possibilities provided in the framework library - there
          are StreamWriter constructors that take a buffer size argument -
          specifying a huge buffer size actually increase performance when
          writing large files.

          Arne




          Comment

          Working...