5 Ways to Make Your Python Code Faster

Give Your Code The Boost It Needs

Adarsh Bulusu
Aug 7, 2020 · 4 min read
Photo by Marc-Olivier Jodoin on Unsplash

all know when it comes to the speed department Python is a much slower language that Java or C. Python is a dynamically-typed language meaning its variable types are not predefined, although, this is a double-edged sword as being dynamically-typed is what makes Python such an elegant language.

So Python is a slower language to run, but faster to type.

Let’s look at some minor tips that could have a major impact on your overall code performance in the long run.

So Python is a slower language to run, but faster to type.

Let’s look at some minor tips that could have a major impact on your overall code performance in the long run.

1| Know Your Built-In Functions

  • This tip helps save your time in the moment and time down the line with your code. When I was beginning Python, I used to never use built-in functions, so to complete my absolute value code I would run a for a loop instead of use abs(). To change a character to uppercase, I would even convert it to the ASCII equivalent of the uppercase letter because I refused to learn string functions.
  • If you’re serious about Python it is worth it to actually study all the built-in Python functions because not only does it make your code neater and more reusable, you can also avoid tiny human inefficiencies in your code by simply using what Python gives you.
  • Link To Built-In Python Functions

2| A,B = B,A

  • I’m sure you’ve used “temp” before as a placeholder variable to swap two elements. What I can tell you is that method belongs in the classroom, and it should not be used when you’re coding.
  • Instead opt for a simple swap of variables by writing them in a,b = b,a order. This switches your variables all in one line and prevents the interpreter from going over three(temp, a,b swap method).
  • This is a tiny fix that saves fractions of a second — but fractions add up in the long run.

3| Only Import Within Your Functions

  • As a beginner, we all like to mass import everything we think we will need at the top of our code. I remember one-time importing NumPy, Pandas, Scipy, Warnings, Math, Math, Os, etc, and by the time I was finished with my code I only used three libraries. This eats up the memory of your computer.
  • Instead, import the needed library within its corresponding function (import multiple times if multiple functions need the same library). This means the interpreter will only complete the import when you call the function rather than at the beginning of the code. Now Python libraries are cached so no it does not take up extra time with each import when you call a different function. However, it does take up more time when you end up importing everything at the top and not even using some of the functions of your code.
  • inefficient program
  • Same code made a bit more efficient

4| Use Numba

  • This a niche tip that mostly helps people who use NumPy or work with scientific programming. Numba is a Python JIT compiler that applied decorators to functions to convert some functions into ultra-fast byte code (almost as fast as C). Numba is starting to make a name for itself purely due to its ease and massive functionality.
  • Numba supports other capabilities right now as well, however, I would say it is largely for scientific Python developers for now.

5| Check from the Set, not the List

  • This is one of the oldest tricks in the Python book of optimizations. Do not go searching in your list if an element exists. Instead, make the list a set ( set(alist) ) and then do your check “ element in set(alist) “. This minor change will improve your runtime efficiency because Python uses resizable hash tables putting the average time complexity at O(1).
  • However, iterating through a set is not faster than iterating through a list.

x in set(alist) → True

x in alist → True (given to you microscopically slower)

Tiny optimizations to your code will snowball and save you a lot of time in the future.

If you enjoyed reading, you can support my work by buying me a cup of coffee!: https://www.buymeacoffee.com/adarshbulusu

The Startup

Get smarter at building your thing. Join The Startup’s +785K followers.

Sign up for Top 10 Stories

By The Startup

Get smarter at building your thing. Subscribe to receive The Startup's top 10 most read stories — delivered straight into your inbox, once a week. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Adarsh Bulusu

Written by

Innovation Enthusiast.

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +785K followers.

Adarsh Bulusu

Written by

Innovation Enthusiast.

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +785K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store