Python Programming Time Complexity

This is an article about time complexity in Python programming. In it we explore what is meant by time complexity and show how the same program can be dramatically more or less efficient in terms of execution time depending on the algorithm used.

Topics covered:

  • What is time complexity in Python programming?
  • “Big O” notation
  • Plotting graphs of time complexity with pyplot

Time complexity is a topic which many self-taught programmers who have not studied Computer Science tend to shy away from. However it is well worth the effort to learn at least the basics of this topic as it will empower you to write much more efficient code.

The subject of Time Complexity in programming can seem a little daunting at first with some unfamiliar mathematical notation and the various graphs which are used to express how the time taken for an algorithm to complete grows as the size of its input grows.


The basic concept of time complexity is simple: looking a graph of execution time on the y-axis plotted against input size on the x-axis, we want to keep the height of the y values as low as possible as we move along the x-axis.

You can get a good intuitive understanding of time complexity by studying the graphs of various mathematical functions and how the height of the graph grows as we move along the x-axis. The graph below shows how various types of mathematical function behave. The idea is that the execution time of algorithms can be seen to grow in a similar fashion to one of these types function, depending on its implementation. Our goal is to write algorithms which behave like the slower growing functions and avoid implementations which behave like the rapidly growing ones.

Python Time complexity

There is a lot of detail that you can go into about whether we are considering the best-case, worst-case, average-case etc, but that is often more detail than you need. To keep it simple, lets just say:

  • exponential: very bad
  • cubic: bad, avoid if possible
  • quadratic: bad, avoid if possible
  • linear: good
  • logarithmic: great
  • constant: you hit the jackpot

Big O notation is a way to refer to these types of growth.

  • O(2ⁿ): exponential
  • O(n³): cubic
  • O(n²): quadratic
  • O(n): linear
  • O(log n): logarithmic
  • O(1): constant

For the remainder of this article, rather than focusing on the general theory of time complexity, we will be looking at a specific algorithm that counts the common elements in a list.

Take a look at this graph:

learn python time complexity

You can clearly see on the graph how the execution time of count_common_elements_nested_loops() grows much more rapidly than for count_common_elements_sets()

it makes use of pyplot from matplotlib, a powerful plotting library for Python. The details of how to use pyplot are for another article, but by examining the code below you can get a sense of how it works. The code uses perf_counter from the time library to calculate the execution time of different algorithms to perform the task of counting common elements is a list. You can see from the resulting graph that there is a significant difference between the implementations in terms of time complexity as the size of the input to each function grows..

Time Complexity Example Python Code Listing

Some observations:

  • The difference in performance is striking, particularly with the speed of growth of the nested for loops version…
  • You might expect list comprehensions to have similar time complexity to nested for loops, since list comprehensions can be created with nested for loops. However, the implementation of list comprehensions “under the hood” is much more efficient.
  • The same applies for sets vs hash_tables, since sets make use of hash_tables. However, The set.intersection method implemented in C. it’s worth remembering that many Built-in functions / methods will almost always be faster than equivalent algorithms that get executed at the python interpreter level.

This article was meant to give you a practical experience of working with time complexity in Python as an introduction to the topic. Time complexity is a big subject and there are many resource available to help you learn online. One place you can get practice is on sites such as hackerrank and project euler, where the “brute force” appraoch may provide correct answers, but not in the required time-frame.

Happy computing!

Leave a Reply

Your email address will not be published. Required fields are marked

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Join our mailing list

Join our mailing list to receive awesome articles about learning Python and Computer Science in a fun and accessible way, straight to your inbox.

 Take your Python skills to the next level!

with our free email course on object oriented programming with Python