$ cat yes3.py stdout = open(1, 'wb') while True: stdout.write(b'y\n') $ python3 yes3.py | pv -r > /dev/null [13.7MiB/s] $ cat yes2.py import os stdout = os.fdopen(1, 'wb') while True: stdout.write('y\n') $ python2 yes2.py | pv -r > /dev/null [7.77MiB/s]
$ python3 yes3.py | pv -r > /dev/null [18.4MiB/s] $ python2 yes2.py | pv -r > /dev/null [10.2MiB/s]
yes2.py:
import os stdout = os.fdopen(1, 'wb') while True: stdout.write('y\n') $ python yes2.py | pv -r > /dev/null [9.12MiB/s] $ pypy yes2.py | pv -r > /dev/null [45.5MiB/s]
Off a quick 9 second run, python2 with profiling:
ncalls tottime percall cumtime percall filename:lineno(function) 1 4.272 4.272 9.301 9.301 yes2.py:1(<module>) 30856080 5.029 0.000 5.029 0.000 {method 'write' of 'file' objects} 1 0.000 0.000 0.000 0.000 {posix.fdopen} 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}