1

Here is my situation.

  1. Read one line from a text file
  2. "Process" the line
  3. Write the "processed" line to a new text file
  4. Loop to #1 and repeat until EOF

Here is how I'm doing it:

using (StreamReader srReader = new StreamReader(strInputFile))
{
    // loop until EOF
    while ((strCurrentLine = srReader.ReadLine()) != null)
    {
        // "process" strCurrentLine...

        // write the "processed" strCurrentLine to a new text file
        using (StreamWriter sw = new StreamWriter(strFullOutputPathFileName, false))
        {
            // write strCurrentLine to the new file
            sw.WriteLine("stuff...");
        }
     }
}

My manager tells me that using the using statement like I am inside of a loop will extremely hinder performance. The reason is because the StreamWriter instance will be created as many times as I'm looping. So, if I looped 1,000 times, I'd have 1,000 instances of my StreamWriter object, which could severely hinder performance.

Is this true? Also, is my method above the best way to accomplish this?

5
  • 7
    Surely the bigger problem than performance is the fact that you're not appending - so your final file will just be the last line of the input file... Commented Mar 27, 2014 at 20:08
  • 5
    I'd have 1,000 instances Well, no. But you'd have created one 1000 times. Commented Mar 27, 2014 at 20:08
  • 2
    The using statement isn't a performance problem, but calling "new StreamWriter" 1000 times instead of once is. As the other comments point out, though, the fact that you are overwriting your file 999 times is a bigger issue. Commented Mar 27, 2014 at 20:10
  • 1
    See C# I/O - Difference between System.IO.File and StreamWriter/StreamReader. I prefer using File.ReadAllText() and File.WriteAllText() over StreamReader and StreamWriter. Commented Mar 27, 2014 at 20:10
  • 2
    @JonSkeet yeah, i am appending...i copied the wrong line Commented Mar 27, 2014 at 20:11

5 Answers 5

7

My manager tells me that using the using statement like I am inside of a loop will extremely hinder performance. The reason is because the StreamWriter instance will be created as many times as I'm looping. So, if I looped 1,000 times, I'd have 1,000 instances of my StreamWriter object, which could severely hinder performance.

Is this true?

Well, it's true, but not because you're creating instances, but because you're opening and closing a file 1,000 times. You could create 1,000 strings with almost no impact to performance.

Also, is my method above the best way to accomplish this?

To start, move the writer creation outside of the while loop:

using (StreamReader srReader = new StreamReader(strInputFile))
{
    // write the "processed" strCurrentLine to a new text file
    using (StreamWriter sw = new StreamWriter(strFullOutputPathFileName, false))
    {
        // loop until EOF
        while ((strCurrentLine = srReader.ReadLine()) != null)
        {
            // "process" strCurrentLine...

            // write strCurrentLine to the new file
            sw.WriteLine("stuff...");
        }
     }
}

However, you could also read the entire file into memory, process it, and write it out in one operation. The impact will depend on the processing that's done and whether you want partial results if there's an error.

Sign up to request clarification or add additional context in comments.

Comments

4

Change your code to:

using (StreamReader srReader = new StreamReader(strInputFile))
{
    using (StreamWriter sw = new StreamWriter(strFullOutputPathFileName, false))
    {
        while ((strCurrentLine = srReader.ReadLine()) != null)
        {
            sw.WriteLine("stuff...");
        }
    }
}

And also, check Jon's comment about appending.

Comments

2

If your file isn't big you can use File.ReadAllLines to get file lines in a string array and do your processing afterwards.

Comments

1

Yes, you will be creating many StreamWriters by doing it that way (not many instances running simultaneously, however). A simple way to solve this is by creating the StreamWriter before you even enter the while loop.

Comments

0

You should open and close the file as few times as possible. So open it (create streamwriter) before the while loop. In the loop use sw.WriteLine("stuff...") After the loop call sw.Close().

Your manager is wrong in sense that it won't create 1000 instances because each instance will be released at the end of the iteration, but you'll be opening and closing the file which will impact the performance.

3 Comments

No, it will create 1000 instances. They won't all be "live" at the same time, but it will definitely create 1000 instances...
Also the claim that each instance will be released at the en of the iteration is imprecise; they may be released then, but there is no guarantee as to when the garbage collector will process them. It will probably happan relatively soon, but unless I'm mistaken, the instances could in theory continue to exist in memory for a while even after they are out of scope.
I agree with both of you Jon and Kjartan.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.