问题
I noticed an unexpected behavior when using F# printfn. It seems to break up the format string into chunks and call Console.Write multiple times for each call to printfn. I would expect it to format the entire string and then call Console.WriteLine once.
I noticed this because I am intercepting the standard Console Output using Console.SetOut call with my own TextWriter which tries to prefix every line of output with a time stamp and some additional custom text.
What gives?
回答1:
Here's my guess, for what it's worth:
- Compiler does some magic
- At runtime, implementer knew that Write will go to a buffered stream, so that performance won't be drastically different from using something like a StringBuilder object and its ToString() override. Maybe performance is even better, since they avoided an object allocation.
来源:https://stackoverflow.com/questions/39459472/why-is-f-printfn-not-implemented-in-terms-of-console-writeline