# 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:
```python
>> 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.
```python
>> 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.
```python
>> sw = Stopwatch()
>> time.sleep(3)
>> sw.print_elapsed_time()
Elapsed time 0:00:03.003280
```
or simply
```python
>> 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:
```python
>> 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:
```python
>> 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
Raw data
{
"_id": null,
"home_page": "https://gitlab.com/luca.baronti/computational-stopwatch",
"name": "computational-stopwatch",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": "computation, time, elapsed time",
"author": "Luca Baronti",
"author_email": "lbaronti@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/4b/fc/03fb0e0299b28883b11d2aebcf879cbedb3c98003b9fcf37cab6a1426066/computational_stopwatch-1.0.5.tar.gz",
"platform": null,
"description": "# Computational Stopwatch\n\nSimple 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.\n\n## Usage\nThe easiest way to use this tool is in conjunction with the **with** python statement:\n```python\n>> from computational_stopwatch import Stopwatch\n>>\n>> with Stopwatch():\n>> time.sleep(3) # <- simulates a computation \nElapsed time 0:00:03.003106\n```\nAnything 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.\n```python\n>> with Stopwatch(\"My short task\"):\n>> time.sleep(3) # <- simulates a computation \nMy short task complete. Elapsed time 0:00:03.003106\n```\nAlternatively to the use with the **with** statment, the class can be directly instantiated and the print function explicitly called.\n```python\n>> sw = Stopwatch()\n>> time.sleep(3)\n>> sw.print_elapsed_time()\nElapsed time 0:00:03.003280\n```\nor simply\n```python\n>> sw = Stopwatch()\n>> time.sleep(3)\n>> print(sw)\n0:00:03.003269\n```\nThe 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.\n\nDifferent **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:\n```python\n>> sw = Stopwatch(verbosity=0)\n>> time.sleep(3)\n>> t = sw.get_elapsed_time()\n>> print(t)\n3.0032315254211426\n```\nBy default, everything is printed on the standard output. Further or alternative streams can be set in the constructor. For instance, the folowing snipped: \n```python\n>> log_file = open('/tmp/my_log_file.txt','w')\n>> with Stopwatch(\"My logged task\", streams=[sys.stdout, log_file]):\n>> time.sleep(3) # <- simulates a computation \nMy logged task complete. Elapsed time 0:00:03.002731\n```\nprints the message both on the standard output as well as in the log file for future perusal.\n\n# Versions History\n\n## v1.0.5\n\n- minor fix\n\n## v1.0.4\n\n- added `HISTORY.md` in the pip package for compatibility with the conda packaging\n\n## v1.0.3\n\n- added ```__str__ ``` function to easily include just the time stamp inside other strings\n\n## v1.0.2\n\n- updated the README\n- added multiple streams functionality\n\n## v1.0.1\n\n- minor fixes\n\n## v1.0.0\n\n- first reelease\n\n\n",
"bugtrack_url": null,
"license": "GNUv3",
"summary": "Simple stopwatch to easily print the elapsed time of a set of operations",
"version": "1.0.5",
"project_urls": {
"Download": "https://pypi.org/project/computational_stopwatch/",
"Homepage": "https://gitlab.com/luca.baronti/computational-stopwatch"
},
"split_keywords": [
"computation",
" time",
" elapsed time"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "f6c6462b9fd9cdd11a106920341e42bd5f2f2794823a5e05469c461fce72e549",
"md5": "1c6ad83d2a764860daf4d06edde1f43d",
"sha256": "81e086d6cea29a548826cb98620f8d25d2f6c465ca91364ba2cc7955864f701d"
},
"downloads": -1,
"filename": "computational_stopwatch-1.0.5-py3-none-any.whl",
"has_sig": false,
"md5_digest": "1c6ad83d2a764860daf4d06edde1f43d",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 4307,
"upload_time": "2024-10-31T18:08:38",
"upload_time_iso_8601": "2024-10-31T18:08:38.577993Z",
"url": "https://files.pythonhosted.org/packages/f6/c6/462b9fd9cdd11a106920341e42bd5f2f2794823a5e05469c461fce72e549/computational_stopwatch-1.0.5-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "4bfc03fb0e0299b28883b11d2aebcf879cbedb3c98003b9fcf37cab6a1426066",
"md5": "626fc270bd88237a9b058a970e20e136",
"sha256": "389ff9c765988aa2abb3019f50962a42bbd653b04ab016c5a36119ebc8858975"
},
"downloads": -1,
"filename": "computational_stopwatch-1.0.5.tar.gz",
"has_sig": false,
"md5_digest": "626fc270bd88237a9b058a970e20e136",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 4084,
"upload_time": "2024-10-31T18:08:39",
"upload_time_iso_8601": "2024-10-31T18:08:39.553763Z",
"url": "https://files.pythonhosted.org/packages/4b/fc/03fb0e0299b28883b11d2aebcf879cbedb3c98003b9fcf37cab6a1426066/computational_stopwatch-1.0.5.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-10-31 18:08:39",
"github": false,
"gitlab": true,
"bitbucket": false,
"codeberg": false,
"gitlab_user": "luca.baronti",
"gitlab_project": "computational-stopwatch",
"lcname": "computational-stopwatch"
}