Skip to main content

Julia Evans

« back to all TILs

Some programming languages buffer stdout and some don't

Today I did some buffering tests in 10 languages where I wrote the equivalent of this program and then ran the program with ./program | grep hello

while True:
    print("hello")
    sleep(1)
  • In 4 of the languages (Python, Ruby, C, Perl) nothing at at all gets printed (because they buffer the output when stdout isn’t a TTY, the buffer size is probably 4k or something)
  • in the others (Go, C++, JS, Rust, Java, Lua, bash) you see “hello” printed out once per second. PHP is in this category too.

I knew there was some variation here I didn’t have a list of which languages did which and now I do. (If you want to not buffer the output you can flush stdout)

Here’s the code I ran to test this