About Measuring Function Execution Time with Timeit

When using timeit to measure function execution time, you will need to use the second parameter for the imports, for example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
import timeit

def function_in_question():
    ...

if __name__ == "__main__":
    print(timeit.timeit(
        """function_in_question()""",
        """from __main__ import function_in_question""",
    ))

Tips and Tricks Programming Optimization Python 3