What are other options for faster io in Python 2.7
Question: I have been struggling with [this](http://www.codechef.com/problems/INTEST)
practice problem on codechef.com for some time. I was finally able to make a
working solution.

    
    
    import sys
    
    def p():
        numbers, div = map(int,sys.stdin.readline().split())
    
        ans = 0
        for i in xrange(numbers):
            if int(sys.stdin.readline()) % div == 0:
                ans += 1
            i += 1
    
        print ans
    
    p()
    

But this executed in 43.60 as is shown
[here](http://www.codechef.com/status/INTEST,anshbansal). It is much worse
than the best given solutions. They are all using psyco module which is not
working for Python 2.7.

Is there some faster method of IO in Python 2.7 which can improve the time-
efficiency of this practice problem and in general programming problems
requiring huge inputs? Please also consider the cases when memory use may be
increased for getting desired time-efficiency.

EDIT:

Faster IO for floating point numbers is not required for this problem but it
may be required for some other problem so suggest something for them also.

EDIT2:

    
    
    nums = int(sys.stdin.readline())
    float_nums = map(float,next(sys.stdin).split())
    for p in islice(sys.stdin, float_nums, None):
    

I was using something like the above for use with @Martijn Pieters answer for
floating point numbers. I am not using `xrange()`


Answer: Use the file as an iterator (different, potentially more optimal buffer
strategy), and take advantage of generator expressions:

    
    
    import sys
    from itertools import islice
    
    def p():
        numbers, div = map(int, next(sys.stdin).split())
        print sum(int(l) % div == 0 for l in islice(sys.stdin, numbers))    
    
    p()
    

This treats booleans as integers (they are a subclass; `True` is 1 in an
integer context, `False` is 0).

Or try:

    
    
    import sys
    from itertools import islice
    
    def p():
        numbers, div = map(int, next(sys.stdin).split())
        print sum(1 for l in islice(sys.stdin, numbers) if int(l) % div == 0)    
    
    p()
    

These do different amounts of work, and depending on the amount of divisible
numbers present, one could be faster than the other (`if` tests versus summing
0s and 1s).

