0

I was trying a coding contest just now.

I was given N lines of inputs are integer, so take those inputs , I used the following code.

arr = [int(input()) for i in xrange(N)]

# where N is a given number of Inputs

Due to this piece of code, I was getting TLE ( TIME LIMIT EXCEEDED ) Error.

But when I change the input code to the following, my code gets accepted without TLE.

arr = []
for i in xrange(N):
    arr.append(int(raw_input()))

#where N is the given number of inputs

Can some please explain, why is there are difference in execution time, though , to my understanding, both the forms of code necessarily do the same task and in the same manner.

Prashant Shrivastava
  • 681
  • 1
  • 11
  • 19
  • 2
    [raw_input vs input](http://stackoverflow.com/questions/3800846/differences-between-input-and-raw-input) – sam Oct 15 '15 at 19:03

1 Answers1

5

The two code snippets are different. One uses raw_input(), the other one uses input(). raw_input() is expected to be faster than input() since it doesn't parse and evaluate the input string as a Python expression.

Sven Marnach
  • 574,206
  • 118
  • 941
  • 841