Professional Documents
Culture Documents
TOC
https://www.loginradius.com/blog/engineering/speed-up-python-code/ 1/8
8/16/22, 12:42 PM Speed Up Python Code | LoginRadius Blog
Python is one of the most popular languages all over the world. Nowadays it is being used
in competitive programming also because of its simple syntax and rich libraries. Most of us
probably started coding with python. At first, everything goes simple and easy. But while
solving a hard algorithmic problem, most of us suffer from Time Limit Exceeded. However, it
is not a problem of python; it is the programmer's problem. I am not saying that language is
not slow, but if a programmer writes an efficient programme, it will get Accepted for sure.
Here are some tips to speed up your python programme.
As for loop is dynamic in python, it takes more time than while loop. So, use while loop
instead of for loop.
L = []
for i in range (1, 1000):
if i%3 == 0:
L.append (i)
https://www.loginradius.com/blog/engineering/speed-up-python-code/ 2/8
8/16/22, 12:42 PM Speed Up Python Code | LoginRadius Blog
a = 2
b = 3
c = 5
d = 7
a, b, c, d = 2, 3, 5, 7
Python has 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.
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.
Use generators
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.
L = []
for element in set(L):
TOC
...
The above code may seem efficient because it used set to delete duplicate data. But the
reality is that the code is not efficient. Do not forget that converting a list into set takes
time. So this code will work better than the previous:
for element in L:
...
import math
val = math.sqrt(60)
https://www.loginradius.com/blog/engineering/speed-up-python-code/ 4/8
8/16/22, 12:42 PM Speed Up Python Code | LoginRadius Blog
Because when you call a function using . (dot) it first calls __getattribute()__ or
__getattr()__ which then use dictionary operation which costs time. So, try using from
module import function.
if a_condition:
if another_condition:
do_something
else:
raise exception
https://www.loginradius.com/blog/engineering/speed-up-python-code/ 5/8
8/16/22, 12:42 PM Speed Up Python Code | LoginRadius Blog
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!
TOC
https://www.loginradius.com/blog/engineering/speed-up-python-code/ 6/8
8/16/22, 12:42 PM Speed Up Python Code | LoginRadius Blog
Featured Posts
Introduction to SolidJS
LOGINRADIUS DOCS
https://www.loginradius.com/blog/engineering/speed-up-python-code/ 7/8
8/16/22, 12:42 PM Speed Up Python Code | LoginRadius Blog
Privacy Policy
Terms
Security Policy
TOC
https://www.loginradius.com/blog/engineering/speed-up-python-code/ 8/8