Multiple Threads#
Why Use Multiple Threads?#
Imagine that you want to troubleshoot which parts of your code are performing better or worse. Or do you want to split-test the performance of different methods? Timer for Python is a quick, easy way to get the job done.
Code Exmaple#
To measure the performance of multiple blocks of code, use the thread
argument to name different threads:
Python | |
---|---|
Terminal output example:
Context Manager#
Or use the context manager with
statement to get the same result with less code:
Python | |
---|---|
Learn more about context management.
Singleton and Unique Threads
The Timer()
class is a singleton, which means that there can only be one instance of the class. This is to ensure that the same Timer()
is used for all threads and that each thread is unique.