Monday, 9 May 2016

TLE (Time Limit Exceeded)

Most of the time when we do coding on online judges we get TLE (Time Limit Exceeded) error message. This is the most basic and most common errors made by programmers. So now we will try to understand what is this TLE about.



What is this TLE ?
 TLE is Time Limit Exceeded error, as name states when time limit exceeds we get this error  message. So now you will be wondering why there is time limit ? Who set this time limit? Answer  is 'problem setter' sets the maximum Time limit for every coding question to prevent the case of  infinite  looping. For example if your code goes in the infinite loop then judge stops execution of  code to  break the loop and exits, giving TLE error message.

Why it happens ?
 Most of the times TLE error occurs when your program is too slow to pass all the test cases in  the specified time limit. You must note that sample test cases given to you are basic test cases  only for testing your code. After submitting code, your code is run on corner cases and much  bigger inputs. If  your program fails to execute on time then still it shows TLE, even your  program  was successfully  able to pass the sample test cases.
 Other reason for TLE could be slow input and output method used on online judges. It depends  on the programming language used. For e.g. Java is slow. 
 
How to prevent TLE ?
 Now a million dollar question is how to write a TLE error free code? You can prevent TLE by    following good programming habits like.
 If you are writing program in C++ instead of using cin/cout you should use scanf/printf.
 If you are writing program in java instead of using Scanner/System.out use  BufferedReader/PrintWriter.
 If you are writing program in python then code can be speed up by writing these two lines in the  starting of code.
    import psyco
    psyco.full()

Now if you still get TLE, brace yourself, you have to think a solution in a different way. you have to optimize your algorithm.

Comment below to discuss. Give suggestion what topic you would like me to write upon.

Happy Coding !!!

No comments:

Post a Comment