Serhiy Storchaka added the comment:

Using readlines() instead of readline() was added in 4dbbf322a9df for 
performance. But it looks that now this is not needed. Naive implementation 
with readline() is about 2 times slower, but with careful optimization we can 
achieve the same performance (or better).

Here are results of benchmarks.

Unpatched:

$ mkdir testdir
$ for i in `seq 10`; do for j in `seq 1000`; do echo "$j"; done 
>"testdir/file$i"; done
$ ./python -m timeit -s "import fileinput, glob; files = 
glob.glob('testdir/*')" -- "f = fileinput.input(files)" "while f.readline(): 
pass"
10 loops, best of 3: 56.4 msec per loop
$ ./python -m timeit -s "import fileinput, glob; files = 
glob.glob('testdir/*')" -- "list(fileinput.input(files))"10 loops, best of 3: 
68.4 msec per loop

Patched:

$ ./python -m timeit -s "import fileinput, glob; files = 
glob.glob('testdir/*')" -- "f = fileinput.input(files)" "while f.readline(): 
pass"
10 loops, best of 3: 47.4 msec per loop
$ ./python -m timeit -s "import fileinput, glob; files = 
glob.glob('testdir/*')" -- "list(fileinput.input(files))"
10 loops, best of 3: 63.1 msec per loop

The patch also fixes original issue.

It also fixes yet one issue. Currently lines are buffered and you need to enter 
many lines first then get first line:

>>> import fileinput
>>> fi = fileinput.input()
>>> line = fi.readline()
qwerty
asdfgh
zxcvbn
^D
>>> line
'qwerty\n'

With the patch you get the line just as it entered.

----------
assignee: docs@python -> serhiy.storchaka
stage: needs patch -> patch review
versions: +Python 3.4, Python 3.5, Python 3.6 -Python 2.7, Python 3.2, Python 
3.3
Added file: http://bugs.python.org/file41224/fileinput_no_buffer.patch

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue15068>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to