How to get REALLY fast python over a simple loop

Posted by totallymike on Stack Overflow See other posts from Stack Overflow or by totallymike
Published on 2010-04-16T04:10:13Z Indexed on 2010/04/16 4:13 UTC
Read the original article Hit count: 181

Filed under:
|
|

I'm working on a spoj problem, INTEST. The goal is to specify the number of test cases (n) and a divisor (k), then feed your program n numbers. The program will accept each number on a newline of stdin and after receiving the nth number, will tell you how many were divisible by k.

The only challenge in this problem is getting your code to be FAST because it k can be anything up to 10^7 and the test cases can be as high as 10^9.

I'm trying to write it in python and having trouble speeding it up. Any ideas?

import sys

first_in = raw_input()
thing = first_in.split()
n = int(thing[0])
k = int(thing[1])
total = 0
i = 0

for line in sys.stdin:
    t = int(line)
    if t % k == 0:
        total += 1

print total

© Stack Overflow or respective owner

Related posts about python

Related posts about speed