In coding competitions we encounter inputs like:
2 3
4 5
So we do the following:
m, n = [int(x) for x in raw_input().split(' ')]
Is there a faster way of doing the same thing?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
For all practical purposes, That’s about as fast as you can get. On some machines, you may see a speedup on the order or a couple percent if you go with
mapinstead of a list comprehension, but that’s not guaranteed.Here’s some quick timings on my machine:
Surprisingly,
split()seems to perform better thansplit(" ").If you’re guaranteed to have ascii input of numbers between 0 and 9, you can do a good bit better using
ord:But that imposes a severe restriction on your inputs.
One other idea that you could try (I have no idea what the performance implications would be), but you could read your lines from
sys.stdin: