The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: Report BugsĬopyright (c) 2020 – present Jakob Bagterp If you have suggestions or changes to the module, feel free to add to the code and create a pull request. And if you like it, feel free to buy me a coffee. However, if you find a bug, please report it. Timer for Python is designed with several nested try/catch clauses so it handles exceptions gracefully and therefore shouldn't break your application while running. Therefore, the output is "humanised" so it's easier to read. And similarly if it runs for hours, it doesn't make sense to display the output time in seconds. If the Timer runs for several minutes, it doesn't make sense to display the output time in milliseconds. Timer for Python supports time measurement from nanoseconds to days.īut. The range is minimum 0 (for no decimals) and up to 9. stop () # Output example: 0.123456789 seconds Default Decimals and Supported Intervalĭefault value for decimals is 2. start ( decimals = 9 ) # Insert your code here timer. Or set the decimals when starting a new thread, which will also override the general decimals defined when initiating the Timer: timer = Timer ( decimals = 5 ) timer. stop () # Output example: 0.12345 seconds To set the number of decimals in the output (only if less than an hour), use the decimals argument.Įither, set the general precision of decimals when initiating the Timer: timer = Timer ( decimals = 5 ) timer. Timer for Python uses the native time.perf_counter_ns() function for maximum resolution in nanoseconds. Or use the with statement to get the same result with less code: with Timer ( thread = "A" ) # Insert your code here with Timer ( thread = "B", decimals = 5 ): # Insert more code here # Output example: 0.12345 seconds for thread B # Insert even more code here # Output example: 6.78 seconds for thread A Documentation and Other Features Precision in Nanoseconds stop ( thread = "A" ) # Output example: 6.78 seconds for thread A stop ( thread = "B" ) # Output example: 0.12345 seconds for thread B # Insert even more code here timer. start ( thread = "B", decimals = 5 ) # Insert more code here timer. start ( thread = "A" ) # Insert your code here timer. To measure performance of multiple blocks of code, use the thread argument to name different threads: timer = Timer () timer. Imagine that you want to troubleshoot which parts of your code are performing better or worse? Or you want to split test the performance of different methods? Timer for Python is a quick, easy way to get the job done. stop () # Output example: 0.12345 seconds Multiple Threads start ( decimals = 5 ) # Insert your code here timer. Instead of the default value 2 for decimals, you can set the output precision up to 9 in the decimals argument: timer = Timer () timer. Or use the benchmark_timer as function decorator: from timer import benchmark_timer def test_function (): # Insert your code here test_function () # Output example: 12.34 seconds for thread TEST_FUNCTION Advanced Decimals Same result with less code: with Timer (): # Insert your code here # Output example: 12.34 seconds Decorator stop () # Output example: 12.34 seconds With StatementĪlternatively, use the with statement, which automatically will stop the Timer. Wrap the Timer function around your code to measure performance of the executed block of code: timer = Timer () timer. If you already have installed the Homebrew package manager for Mac and Linux, execute this terminal command to tap Timer for Python: brew tap jakob-bagterp/timer-for-pythonĪnd then install: brew install timer-for-pythonįirstly, add the Timer to your imports: from timer import Timer If you already have installed Timer for Python, use this command to upgrade to latest version: pip3 install -upgrade timer-for-python PrerequisitesĪssuming that Python is installed already, execute this command in the terminal: pip3 install timer-for-python Lightweight Python package that makes it easy to measure how much time it takes to run Python programs and gauge performance of multiple, smaller bits of code.
0 Comments
Leave a Reply. |