tags:

views:

259

answers:

3

I have a requirement to write HTML to the file system and I was wondering if there is any speed boost in converting it to bytes and writing it using a FileStream rather than using the File.WriteAllText() (or a similar text method).

+10  A: 

What do you think happens within File.WriteAllText? In the end, it is going to be bytes hitting the disk. That disk is not unlikely to be the slow part in this chain anyway. Unless the data being written is very large, I would not worry about it, and just use the implementation that I felt was most convenient. If you design the code well it will not be a major thing to change later, if needed.

Fredrik Mörk
I don't have a bottle neck, I was just wondering since the processor is time sensitive we are trying to save in all the areas possible. Thanks for your response though.
Rob
+7  A: 

Write the code that is most readable and maintainable first. Then in the future if you have a performance problem, look for the bottle neck.

(I'd be very surprised if the bottle neck turns out to be anything to do with converting the string to a byte array or not. The bottle neck with something like this will be your disk write speed)

Don't optimise prematurely.

Simon P Stevens
+1. First make it work, re-design if necessary and then fine tune it.
Vivek
I'm guessing he's asking because he's made it work and is now re-designing. Also this answer contradicts the first! Disk writes are expensive, but also two copies of the string in memory isn't a good idea for a server application
Chris S
@Chris: How does it contradict the first one (Fredrik's?) , we both say pretty much the same thing which is go for maintainability first, and that the main time cost is most likely the disk anyway. Or have I misread Fredricks answer?
Simon P Stevens
I meant one says disk writes are fast, yours says they're not (which is normally the commonest advice)
Chris S
@Chris: Ahh, Fredrik says "disk is not unlikely to be the slow part". I took that as a double negative to mean 'disk is likely to be the slow part'. I would assume that is what he means as disks are slow when compared to the CPU.
Simon P Stevens
+2  A: 

File.WriteAllText uses a StreamWriter behind the scenes:

public static void WriteAllText(string path, string contents, Encoding encoding)
{
    using (StreamWriter writer = new StreamWriter(path, false, encoding))
    {
        writer.Write(contents);
    }
}

You have a string already, so converting it to a byte array would be pointless, because this is performed inside the StreamWriter.Flush method anyway using its underlying Encoder class. Flush is called by the StreamWriter.Dispose method which the using clause calls. This is a snippet from the source of Flush via reflector:

int count = this.encoder.GetBytes(this.charBuffer, 0, this.charPos, this.byteBuffer, 0, flushEncoder);

You can see it has a charBuffer. That's just a char[] array which it writes to when you perform StreamWriter.Write(string).

So in short, you already have the string, just let the shorter File method perform the cascading calls, as it makes your source slightly more readable. Converting it to a byte array is also not necessary, as I mentioned the StreamWriter does it for you.

If you're getting conversion issues then use Encoding.Unicode as the final parameter in the 3 parameter overload, as File.WriteAllText(string,string) uses UTF8 without a BOM.

Chris S
Thank you for actually answering the question. Everyone else, take note of this and follow his lead. Making statements that have nothing to do with the actual question is pointless and just gets in the way.
Rob