PyPy Output

Правка en1, от avamsi, 2016-10-11 12:05:56

For some reason, the output is not buffered in PyPy (unlike CPython).
So, for problems where you use lots of print statements (especially in loops), unbuffered output has a significant effect on overall run-time performance.

For example, compare these 2 submissions -- 21354004, 21354013.
Both solutions are exactly the same except in the latter, stdout is replaced by a buffer and all the output stored in the buffer is written to original stdout at the end.

21354004 times out (1000ms TL) even though everything except buffering is same as 21354013.
21354013 runs in 360ms -- at least 3x faster than the unbuffered solution.

For explicit unbuffered output, prefix your code with following code like in 21354013

import atexit
import io
import sys

buff = io.BytesIO()
sys.stdout = buff


@atexit.register
def write():
    sys.__stdout__.write(buff.getvalue())


# code
Теги python, python 2, pypy

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en4 Английский avamsi 2017-03-31 09:09:47 2 Tiny change: ' explicit unbuffered o' -> ' explicit buffered o'
en3 Английский avamsi 2016-10-12 17:07:00 18 Tiny change: 'n PyPy (un[user:hellman1908]like CPyth' -> 'n PyPy (unlike CPyth'
en2 Английский avamsi 2016-10-12 17:06:02 459 Tiny change: 'nt-320135):\n\n```\ni' -> 'nt-320135)\n\n```\ni'
en1 Английский avamsi 2016-10-11 12:05:56 1274 Initial revision (published)