Re:A Little Story About the `yes` Unix Command
by glandium on Wednesday October 11, 2017 @09:35PM
(#15455041)
Your bytes version outputs lines of, literally, `b'y'`.
The characters versions is still a clear win for python2 on my machine (8.9MiB/s vs. 5.6MiB/s)
It's also worth noting that the buffering behavior of python is only happening because the output is a pipe to pv. If it were the terminal, it would be line buffered, like the naive rust version.
Re:A Little Story About the `yes` Unix Command
by thechewanater on Wednesday October 11, 2017 @10:31PM
(#15455254)
python3 seems to do much better than either of those for me when using an unbuffered write(1, ...) syscall (plus it prints the correct thing)
$ 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]
Re:A Little Story About the `yes` Unix Command
by glandium on Wednesday October 11, 2017 @10:43PM
(#15455302)
For better comparison with my numbers, I ran your scripts on my machine:
$ python3 yes3.py | pv -r > /dev/null
[18.4MiB/s]
$ python2 yes2.py | pv -r > /dev/null
[10.2MiB/s]
In both cases, a 4KiB buffer is used by python. That's still way slower than the equivalent rust code with a 4KiB buffer (use BufWriter::with_capacity(4096, stdout.lock()) instead of BufWriter::new(stdout.lock())).
Re:A Little Story About the `yes` Unix Command
by Twirrim on Wednesday October 11, 2017 @11:54PM
(#15455550)
Out of curiosity:
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]
So pypy does a good job of speeding it up.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}
Does it have nothing to do with the fact that string-of-bytes is the default in Python 2, whereas string-of-characters is the default in Python 3? Or is that perhaps related to the explanation you gave? Forcing the byte interpretation, Python 3 is slightly faster than Python 2 for me. Forcing the character interpretation, Python 2 wins, but not by as much as before.
Bytes:
Characters: