January 2019
Beginner
318 pages
8h 23m
English
timeit is a Python module used to time small parts of your Python script. You can call timeit from the command line as well as import the timeit module into your script. We are going to write a script to time a piece of code. Create a timeit_example.py script and write the following content into it:
import timeitprg_setup = "from math import sqrt"prg_code = '''def timeit_example(): list1 = [] for x in range(50): list1.append(sqrt(x))'''# timeit statementprint(timeit.timeit(setup = prg_setup, stmt = prg_code, number = 10000))
Using timeit, we can decide what piece of code we want to measure the performance of. So, we can easily define the setup code as well as the code snippet on which we want to perform the test separately. The main ...
Read now
Unlock full access