Package: timer-for-python
⏳ 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.Ready to try? Learnhow to installand find tutorials in theuser guide.Getting StartedBasicsSimply add the Timer to your imports, and then wrap the Timer function around your code to measure the performance of the executed block of code:fromtimerimportTimertimer=Timer()timer.start()# Insert your code heretimer.stop()# Output example: 12.34 secondsContext ManagerAlternatively, use the with statement. This will automatically start and stop the Timer – and so no need to declaretimer.start()andtimer.stop(). Same result as before, but less code:withTimer():# Insert your code here# Output example: 12.34 secondsDecoratorOr use thebenchmark_timeras a function decorator:fromtimerimportbenchmark_timer@benchmark_timerdeftest_function():# Insert your code heretest_function()# Output example: 12.34 seconds for thread TEST_FUNCTIONCore FeaturesDecimalsInstead of the default value of2fordecimals``, you can set the output precision up to9in thedecimals` argument:timer=Timer()timer.start(decimals=5)# Insert your code heretimer.stop()# Output example: 0.12345 secondsMultiple ThreadsImagine 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.To measure the performance of multiple blocks of code, use thethreadargument to name different threads:timer=Timer()timer.start(thread="A")# Insert your code heretimer.start(thread="B",decimals=5)# Insert more code heretimer.stop(thread="B")# Output example: 0.12345 seconds for thread B# Insert even more code heretimer.stop(thread="A")# Output example: 6.78 seconds for thread AOr use thewithstatement to get the same result with less code:withTimer(thread="A")# Insert your code herewithTimer(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 AThank You for SupportingDonateThis module is free to use. And if you like it, feel free tobuy me a coffee.ContributeIf you have suggestions or changes to the module, feel free to add to the code and create apull request.Report BugsReport bugs and issueshere.MIT LicenseCopyright (c) 2020 – present, Jakob BagterpPermission 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:The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.