KEEP K.I.S.S.

tk's blog

屏幕 IO 是很慢的

tk posted @ May 25, 2012 03:56:02 PM in C , 2001 阅读

今天测试写的一个 lua 脚本,用于文件解析和输出报表,然后发现输出很慢,差不多要 400s 左右的时间,输出的行数越 10万行,总数据量 1.2MB。

然后发觉自己在运行命令里关掉了 stdout 的 buffer,但是开启后并没有多大的速度提升,然而,如果把输出重定向(cmd /c xxx > output.txt)后,速度有了质变,只需要 0.31s,其中 IO 只用了 0.17s。

然后我就 google 了一下,发现了这个解释,:http://stackoverflow.com/questions/3857052/why-is-printing-to-stdout-so-slow-can-it-be-sped-up(Why is printing to stdout so slow? Can it be sped up?)

大意就是 stdout(仅指屏幕 IO) 的实现里大多都是无缓冲或者小缓冲(行缓冲),所以大量的输出时候会很慢。

-------

多谢 依云兄的指正

  • 无匹配
  • 无匹配
Avatar_small
依云 说:
May 26, 2012 12:23:22 AM

这个不叫「stdout 很慢」,是「输出到终端或者屏幕很慢」,这时候使用的是行缓冲。而重定向到文件则使用全缓冲。stdout 还是 stdout。

Avatar_small
tisyang 说:
May 27, 2012 04:17:21 PM

@依云: 你纠正的对,我没有仔细区分这个~

egmkang 说:
Oct 24, 2012 04:27:50 PM

你可以试试直接向stdout这个file里面write

NCERT Question Paper 说:
Sep 20, 2022 01:38:44 PM

Students studying at CBSE, KVS, JNV and other Central & State Education Boards of the country may use the NCERT Sample Paper 2023 Pdf Download for 1st, 2nd, 3rd, 4th, 5th, 7th, 8th, 9th, 10th, 11th & 12th Standard Sample Paper Suggestions. NCERT Question Paper Pdf To access the course's main downloading page, click on the links provided. Class-wide sample practise question banks are recommended for all languages and subjects.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter