What is the fastest way to do I/O in Python?
Question: Like those programming challenges, right now I do the following:

For a single variable:

    
    
    x = int(sys.stdin.readline())
    

for many variables

    
    
    A, B, C = map(int,sys.stdin.readline().split())
    

Is this optimal or are there faster ways?


Answer: If you have numpy available, the numpy loading functions are very fast. For
example:

    
    
    >>> import numpy
    >>> s = '1\n2\n3\n4\n'
    >>> data = numpy.fromstring(s, dtype=int, sep='\n')
    >>> data
    array([1, 2, 3, 4])
    

This example loads from a string, but you can also load directly from an open
file using
[numpy.fromfile](http://docs.scipy.org/doc/numpy/reference/generated/numpy.fromfile.html#numpy.fromfile).

