Simple stopwatch to easily print the elapsed time of a set of operations
Project description
Computational Stopwatch
Simple stopwatch to easily print the elapsed time of a set of operations. It's a minimalistic library, but it is very useful in many real cases.
Usage
The easiest way to use this tool is in conjunction with the with python statement:
>> from computational_stopwatch import Stopwatch
>>
>> with Stopwatch():
>> time.sleep(3) # <- simulates a computation
Elapsed time 0:00:03.003106
Anything within the scope of the with statement will count against the elapsed time. An optional task name to be printed along the elapsed time (e.g. for better identification in a log) can be set in the constructor. This name will be prepended to the printed message. This is useful to track the elapsed time of several tasks ran in sequence.
>> with Stopwatch("My short task"):
>> time.sleep(3) # <- simulates a computation
My short task complete. Elapsed time 0:00:03.003106
Alternatively to the use with the with statment, the class can be directly instantiated and the print function explicitly called.
>> sw = Stopwatch()
>> time.sleep(3)
>> sw.print_elapsed_time()
Elapsed time 0:00:03.003280
or simply
>> sw = Stopwatch()
>> time.sleep(3)
>> print(sw)
0:00:03.003269
The start time can be reset with the reset_time function and the get_elapsed_time method returns the unformatted elapsed time, which is useful for numerical comparisons.
Different verbosity levels can be set in the constructor, with 2 as the default level, with 1 only the time is printed when the object is deleted, and with 0 nothing is printed. This is convenient to directly assess the elapsed time in seconds without any rogue prints on deletion:
>> sw = Stopwatch(verbosity=0)
>> time.sleep(3)
>> t = sw.get_elapsed_time()
>> print(t)
3.0032315254211426
By default, everything is printed on the standard output. Further or alternative streams can be set in the constructor. For instance, the folowing snipped:
>> log_file = open('/tmp/my_log_file.txt','w')
>> with Stopwatch("My logged task", streams=[sys.stdout, log_file]):
>> time.sleep(3) # <- simulates a computation
My logged task complete. Elapsed time 0:00:03.002731
prints the message both on the standard output as well as in the log file for future perusal.
Versions History
v1.0.5
- minor fix
v1.0.4
- added
HISTORY.md
in the pip package for compatibility with the conda packaging
v1.0.3
- added
__str__
function to easily include just the time stamp inside other strings
v1.0.2
- updated the README
- added multiple streams functionality
v1.0.1
- minor fixes
v1.0.0
- first reelease
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file computational_stopwatch-1.0.5.tar.gz
.
File metadata
- Download URL: computational_stopwatch-1.0.5.tar.gz
- Upload date:
- Size: 4.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 389ff9c765988aa2abb3019f50962a42bbd653b04ab016c5a36119ebc8858975 |
|
MD5 | 626fc270bd88237a9b058a970e20e136 |
|
BLAKE2b-256 | 4bfc03fb0e0299b28883b11d2aebcf879cbedb3c98003b9fcf37cab6a1426066 |
File details
Details for the file computational_stopwatch-1.0.5-py3-none-any.whl
.
File metadata
- Download URL: computational_stopwatch-1.0.5-py3-none-any.whl
- Upload date:
- Size: 4.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 81e086d6cea29a548826cb98620f8d25d2f6c465ca91364ba2cc7955864f701d |
|
MD5 | 1c6ad83d2a764860daf4d06edde1f43d |
|
BLAKE2b-256 | f6c6462b9fd9cdd11a106920341e42bd5f2f2794823a5e05469c461fce72e549 |