Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a faster way to get input in python?

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?

like image 615
pyronic Avatar asked Oct 08 '12 14:10

pyronic


People also ask

What is the fastest way to read input in Python?

Fast Input So try reading the input directly from the Judge's file using the Operating system(os) module, and input/output (io) module. This reading can be done in the form of bytes. By using this method, integer input works normally, but for string input, it will store the string as a byte like an object.

How do you speed up code in Python?

Try Multiprocessing Python is generally limited to a single core when processing code, but using the multiprocessing library allows us to take advantage of more than one. In very CPU-bound problems, dividing the work across several processors can really help speed things up.

Is Stdin faster than input?

stdin. readline() is the fastest one when reading strings and input() when reading integers.


1 Answers

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 map instead of a list comprehension, but that's not guaranteed.

Here's some quick timings on my machine:

from itertools import imap
#map
>>> timeit.timeit('x,y = map(int,line.split(" "))','from __main__ import line')
4.7857139110565186
>>> timeit.timeit('x,y = map(int,line.split())','from __main__ import line')
4.5680718421936035
#list comprehension
>>> timeit.timeit('x,y = [int(x) for x in line.split(" ")]','from __main__ import line')
4.3816750049591064
>>> timeit.timeit('x,y = [int(x) for x in line.split()]','from __main__ import line')
4.3246541023254395
#itertools.imap
>>> timeit.timeit('x,y = imap(int,line.split(" "))','from __main__ import line,imap')
4.431504011154175
>>> timeit.timeit('x,y = imap(int,line.split())','from __main__ import line,imap')
4.3257410526275635
#generator expression
>>> timeit.timeit('x,y = (int(x) for x in line.split(" "))','from __main__ import line')
4.897794961929321
>>> timeit.timeit('x,y = (int(x) for x in line.split())','from __main__ import line')
4.732620000839233

Surprisingly, split() seems to perform better than split(" ").


If you're guaranteed to have ascii input of numbers between 0 and 9, you can do a good bit better using ord:

>>>timeit.timeit('x,y = [ord(x)-48 for x in line.split(" ")]','from __main__ import line')
1.377655029296875
>>> timeit.timeit('x,y = [ord(x)-48 for x in line.split()]','from __main__ import line')
1.3243558406829834

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:

import sys
for line in sys.stdin:
    x,y = [ord(x)-48 for x in line.split()]
like image 86
mgilson Avatar answered Oct 03 '22 10:10

mgilson