As we learn to use code to solve all types of problems in the world, questioning the efficiency of our programs becomes increasingly important. In this tutorial, you'll learn how to analyze your Python programs' speed and efficiency so you can write fast code!
Let's look at an example where we can see the differences between two programs that do the same exact thing: given a list of numbers, determine if there are any duplicates.
def duplicates_list(lst): """ Returns whether or not there are duplicate values in LST. """ seen_so_far = [] for num in lst: if num in seen_so_far: return True seen_so_far.append(num) return False def duplicates_set(lst): """ Returns whether or not there are duplicate values in LST. """ return len(lst) != len(set(lst))
Check out these methods in action below!
As we can see, the second duplicate function is much faster than the first. Why is this the case? Well, we'll go into more depth on this in another tutorial, but for now, it is important to recognize the different data structures the two methods employ; duplicates_list uses an array while duplicates_set uses a hash set.
Before diving into the mathematical understanding of why duplicates_set is significantly faster, try using the "time" library on your own projects or activities and compare different implementations of functions like done here! Share your programs with your friends and see if they can write faster functions!
As a reminder, here are the concrete steps to testing the speed of a function:
import time
Calculate start time
start = time.time()
Call your function,
duplicates(lst)
Calculate end time
end = time.time()
Compute elapsed time
elapsed_seconds = end - start
Happy testing!