Line-buffered output aims to make terminal messages appear promptly by flushing whenever a newline is written, but this can generate many small system calls and slow programs that produce lots of brief status lines. Fully buffered output avoids that overhead, yet may keep useful information hidden until the buffer fills or the program explicitly flushes. Archive extraction illustrates the cost: printing each filename can dominate the time spent unpacking many small files. The behavior of common runtimes also varies; glibc may issue separate writes for lines within one string, while CPython can combine them but still flushes as soon as its buffer contains a newline.
A better compromise is to flush when output has been idle for a short, human-imperceptible interval, such as 100 milliseconds, rather than treating every newline as a trigger. A buffer can track a deadline and refresh it as more output arrives; if work continues past the deadline, the program flushes. This batches lines produced close together into fewer writes while still showing progress during longer tasks. The approach requires the caller to check the deadline between short stretches of work, but does not require a background thread. In an unzip loop, for example, filenames can accumulate while each file is processed, then appear promptly if extraction takes long enough. Users retain visible progress without paying the usual line-buffering penalty.
Summary generated by AI from the linked article. hn.today is not affiliated with Hacker News or Y Combinator.