Aan de slagGa gratis aan de slag

Finding an element

Using the right data structure can significantly increase the performance of your code. For example, if you want to find a certain element in the data, you might want to choose between list and set. In this exercise, you will implement performance tests with pytest to compare the speed of the in operator applied correspondingly to the two data structures: list and set. The pytest package has already been imported.

Deze oefening maakt deel uit van de cursus

Introduction to Testing in Python

Cursus bekijken

Oefeninstructies

  • Pass benchmark as an argument into the test functions.
  • Then call benchmark() in the test functions passing find() as the first argument.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

def create_list():
    return [i for i in range(1000)]
def create_set():
    return set([i for i in range(1000)])
def find(it, el=50):
    return el in it

# Write the performance test for a list
def test_list(____):
    ____(____, ____)

# Write the performance test for a set
def test_set(____):
    ____(____, ____)
Code bewerken en uitvoeren