Python is one of the most popular programming languages all over the world. For its, simple syntax and rich library, it is being used in competitive programming also. Most of us probably started coding with python. At first, everything goes simple and easy. But while solving a hard algorithmic problems, most of us suffer from
Time Limit Exceeded. However, it is not a problem of python; most of the cases, the problem is with the programmer. I am not saying that python is not slow, but if a programmer writes efficient programme, it will get
Accepted for sure. Here are some tips to speed up your python programme.
Use of proper data structure has a significant effect on runtime. Python has list, tuple, set and dictionary as the built-in data structures. However, most of the people use the list in all cases. But it is not a right choice. Use proper data structures depending on your task. Especially use a tuple instead of a list. Because iterating over tuple is easier than iterating over a list.
As for loop is dynamic in python, it takes more time than while loop. So, use while loop instead of for loop.
Do not use any other technique if you can use list comprehension. For example, here is a code to list all the numbers between 1 and 1000 that is the multiplier of 3:
L =  for i in range (1, 1000): if i%3 == 0: L.append (i)
Using list comprehension, it would be:
L = [i for i in range (1, 1000) if i%3 == 0]
List comprehension works faster than using the append method.
Do not assaign variables like this:
a = 2 b = 3 c = 5 d = 7
Instead, assign variables like this:
a, b, c, d = 2, 3, 5, 7
global keyword to declare global variables. But global variables take higher time during operation than a local variable. So, do not use global variables if it is not necessary.
Do not write your function (manually) if it is already in the library. Library functions are highly efficient, and you will probably won’t be able to code with that efficiency.
concatenatedString = "Programming " + "is " + "fun."
It can also be done with
concatenatedString = " ".join (["Programming", "is", "fun."])
join() concatenates strings faster than
+ operation because
+ operators create a new string and then copies the old content at each step. But
join() doesn’t work that way.
If you have a large amount of data in your list and you need to use one data at a time and for once then use
generators. It will save you time.
Try to avoid dot operation. See the below programme.
import math val = math.sqrt(60)
Instead of the above style write code like this:
from math import sqrt val = sqrt(60)
Because when you call a function using
. (dot) it first calls
__getattr()__ which then use dictionary operation which costs time. So, try using
from module import function.
while 1 instead of
while True. It will reduce some runtime.
Try new ways to write your code efficiently. See the below code.
if a_condition: if another_condition: do_something else: raise exception
Instead of the above code you can write:
if (not a_condition) or (not another_condition): raise exception do_something
For python’s slow speed, some projects have been taken to decrease runtime. Pypy and Numba two of them. In most of the programming contests, you will see pypy if it allows python. These applications will reduce the runtime of your programme.
C/C++ is faster than python. So, many packages and modules have been written in C/C++ that you can use in your python programme.
Pandas are three of them and are popular for processing large datasets.
Python is updated and upgraded regularly, and every release is faster and more optimized. So always use the latest version of python.
These were some of the tips to decrease the runtime of python code. There are a few more techniques that you can use. Use a search engine to find those and write efficient code!