Enormous Input and Output Test

Chris Rebert clp2 at rebertia.com
Sun Oct 4 01:29:36 CEST 2009

On Sat, Oct 3, 2009 at 6:54 AM, n00m <n00m at narod.ru> wrote:
> Hi, py.folk!
> I need your help to understand how
> http://www.spoj.pl/problems/INOUTEST/
> can be passed in Python.
> def foo():
>    ##sys.stdin = open('D:/1583.txt', 'rt')
>    a = sys.stdin.readlines()

That line is probably a Very Bad Idea (TM) as it reads the *entire*
enormous file into memory *at once*. It would probably be much better
to iterate over the file, thus only reading one individual line at a
time. I'm betting the massive malloc()ing involved with .readlines()
is a large part of the slowness.

> But it gets "Time Limit Exceeded" verdict.
> Any ideas?


More information about the Python-list mailing list