https://github.com/nok/tic-toc
Measure and track the wall and CPU time of defined scopes.
https://github.com/nok/tic-toc
python time timeit utils
Last synced: 2 months ago
JSON representation
Measure and track the wall and CPU time of defined scopes.
- Host: GitHub
- URL: https://github.com/nok/tic-toc
- Owner: nok
- License: mit
- Created: 2018-10-21T13:50:57.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-12-02T22:33:18.000Z (over 6 years ago)
- Last Synced: 2024-11-12T09:27:59.911Z (7 months ago)
- Topics: python, time, timeit, utils
- Language: Python
- Homepage:
- Size: 21.5 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license.txt
Awesome Lists containing this project
README
# tic-toc
[](https://pypi.python.org/pypi/tic-toc)
[](https://pypi.python.org/pypi/tic-toc)
[](https://raw.githubusercontent.com/nok/tic-toc/master/license.txt)Measure and track the wall and CPU time of defined scopes in Python.
It's suitable for long-running applications which should be monitored.## Installation
```bash
$ pip install tic-toc
```## Usage
The following examples demonstrate the most simple usage:
Here you see the implicit variant with the `with` construct:
```python
from tic_toc import Timerwith Timer('NAME') as timer:
print('Scope: ' + timer.name)
time.sleep(1)
```Here you see the explicit variant without the `with` construct:
```python
from tic_toc import Timertimer = Timer('NAME')
timer.tic() # or .start()
print('Scope: ' + timer.name)
time.sleep(1)
timer.toc() # or .end()
```The outputs are similar to each other:
```python
# > NAME ...
# Scope: NAME
# < NAME [WALL: 1.0034s] [CPU: 0.0001s]
```You can find more extended examples (e.g. with `logging`, `tqdm` or `pandas`) in the [examples](examples) directory.
## Parameters
```python
class Timer(object):
def __init__(self, name: str = None,
format_start: str = '> {name} ...',
format_end: str = '< {name} [WALL: {time_wall:.4f}s] [CPU: {time_cpu:.4f}s]',
to: Callable[[Any], None] = lambda msg: print(msg)):
```- `name`: str, optional, default: leading hash with four random digits, eg. `#0512`
- `format_start`: str, optional, default: `'> {name} ...'`
- `format_end`: str, optional, default: `'< {name} [WALL: {time_wall:.4f}s] [CPU: {time_cpu:.4f}s]'`
- `to`: `Callable[[Any], None]`, optional, default: `lambda msg: print(msg)`You can change the visual string formats and the output method.
## Sources
- [Answer](https://stackoverflow.com/a/5849861/1293700) by Eli Bendersky to the question '[tic, toc functions analog in Python](https://stackoverflow.com/questions/5849800/tic-toc-functions-analog-in-python)'
- [Python Clocks Explained](https://www.webucator.com/blog/2015/08/python-clocks-explained/) by Nat Dunn
- [Python 3.7 perf_counter() nanoseconds](https://vstinner.github.io/python37-perf-counter-nanoseconds.html) by Victor Stinner
- [PEP 564: New Time Functions With Nanosecond Resolution](https://docs.python.org/3.7/whatsnew/3.7.html#pep-564-new-time-functions-with-nanosecond-resolution)
- [PEP 560: Core Support for typing module and Generic Types](https://docs.python.org/3.7/whatsnew/3.7.html#pep-560-core-support-for-typing-module-and-generic-types)
- [Slow and fast methods for generating random integers in Python](https://eli.thegreenplace.net/2018/slow-and-fast-methods-for-generating-random-integers-in-python/) by Eli Bendersky## License
The module is Open Source Software released under the [MIT](license.txt) license.