#!/usr/bin/python import sys # This is the only truely line by line input python has. # Other methods mentioned below are faster, but don't # consume a line at a time, so can't be used for this test. while 1: line = sys.stdin.readline() if line == '': break try: print line, except: pass # This is the fastest method (over twice as fast as above), # and supported since python 2.2 #for line in sys.stdin: # try: # print line, # except: # pass # Interesting discussion about efficiency on http://www.python.org/dev/summary/2001-01-1/ # (uses fgets). Also http://www.dabeaz.com/blog/2010/01/reexamining-python-3-text-io.html # suggests that for fast text I/O on python 3 one needs to do: # sys.stdin = os.fdopen(sys.stdin.fileno(), "rb") # for line in sys.stdin.readlines(): # try: # print line, # except: # pass # This should be twice as fast as the above while: # (actually a little better than twice as fast in my tests) # for line in sys.stdin.xreadlines(): # print line # The following is from python docs # import xreadlines, sys # for line in xreadlines.xreadlines(sys.stdin): # pass # has approximately the same speed and memory consumption as # while 1: # lines = sys.stdin.readlines(8*1024) #Note this is a hint (see below) # if not lines: break # for line in lines: # pass #And even more info from the 2.1 release docs: # Even if you don't use file.xreadlines(), you may expect a speedup on # line-by-line input. The file.readline() method has been optimized # quite a bit in platform-specific ways: on systems (like Linux) that # support flockfile(), getc_unlocked(), and funlockfile(), those are # used by default. On systems (like Windows) without getc_unlocked(), # a complicated (but still thread-safe) method using fgets() is used by # default. # # You can force use of the fgets() method by #define'ing # USE_FGETS_IN_GETLINE at build time (it may be faster than # getc_unlocked()). # # You can force fgets() not to be used by #define'ing # DONT_USE_FGETS_IN_GETLINE (this is the first thing to try if std test # test_bufio.py fails -- and let us know if it does!). # #- In addition, the fileinput module, while still slower than the other # methods on most platforms, has been sped up too, by using # file.readlines(sizehint).