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.
Latihan ini adalah bagian dari kursus
Introduction to Testing in Python
Petunjuk latihan
- Pass
benchmarkas an argument into the test functions. - Then call
benchmark()in the test functions passingfind()as the first argument.
Latihan interaktif praktis
Cobalah latihan ini dengan menyelesaikan kode contoh berikut.
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(____):
____(____, ____)