为什么在Console.WriteLine方面未实现F#printfn? [英] Why is F# printfn not implemented in terms of Console.WriteLine?

查看:84
本文介绍了为什么在Console.WriteLine方面未实现F#printfn?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用F#printfn时,我注意到了意外的行为. 似乎将格式字符串分解为多个块并调用Console.对printfn的每次调用多次写入.我希望它能格式化整个字符串,然后一次调用Console.WriteLine.

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.

之所以注意到这一点,是因为我使用自己的TextWriter通过Console.SetOut调用拦截了标准Console输出,该TextWriter尝试在输出的每一行前添加时间戳和一些其他自定义文本.

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.

有什么作用?

推荐答案

这是我的猜测,它的价值在于:

Here's my guess, for what it's worth:

  1. 编译器发挥了神奇作用
  2. 在运行时,实现者知道Write将进入缓冲流,因此与使用诸如StringBuilder对象及其ToString()覆盖之类的东西相比,性能不会有太大不同.也许性能会更好,因为他们避免了对象分配.

这篇关于为什么在Console.WriteLine方面未实现F#printfn?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆