Skip to content

Commit 76841d8

Browse files
committed
Version 1.0
1 parent 20acf46 commit 76841d8

4 files changed

Lines changed: 143 additions & 1 deletion

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*.pyc
2+
dist/
3+
timer_cm.egg-info/

README.md

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,47 @@
11
# timer-cm
2-
A Python context manager for measuring execution time
2+
A Python context manager for measuring execution time.
3+
4+
## Installation
5+
6+
```
7+
pip install timer_cm
8+
```
9+
10+
## Usage
11+
First, import the `Timer` class:
12+
13+
```python
14+
from timer_cm import Timer
15+
```
16+
17+
Then use it as a context manager. In this example we use Python's `sleep(...)` function to pause execution for one second:
18+
19+
```python
20+
with Timer('Simple task'):
21+
sleep(1)
22+
```
23+
24+
This produces the following output:
25+
26+
```
27+
Simple task: 1.005s
28+
```
29+
30+
Often you want to know where a long running code block spends its time. Use `timer.child(name)` to track individual steps:
31+
32+
```python
33+
with Timer('Long task') as timer:
34+
with timer.child('First step'):
35+
sleep(1)
36+
for _ in range(5):
37+
with timer.child('Many small steps'):
38+
sleep(.5)
39+
```
40+
41+
Output:
42+
43+
```
44+
Long task: 3.520s
45+
Many small steps: 2.518s (71%)
46+
First step: 1.001s (28%)
47+
```

setup.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"""Measure execution time with a context manager
2+
3+
See:
4+
https://github.com/mherrmann/timer-cm
5+
"""
6+
7+
from setuptools import setup
8+
9+
description = 'Measure execution time with a context manager.'
10+
setup(
11+
name='timer-cm',
12+
13+
version='1.0',
14+
15+
description=description,
16+
long_description=
17+
description + '\n\nHome page: https://github.com/mherrmann/timer-cm',
18+
url='https://github.com/mherrmann/timer-cm',
19+
20+
author='Michael Herrmann',
21+
author_email='[my first name]@[my last name].io',
22+
23+
license='MIT',
24+
25+
platforms=['MacOS', 'Windows', 'Debian'],
26+
27+
classifiers=[
28+
'Development Status :: 5 - Production/Stable',
29+
'Intended Audience :: Developers',
30+
31+
'License :: OSI Approved :: MIT License',
32+
33+
'Operating System :: OS Independent',
34+
35+
'Programming Language :: Python',
36+
'Programming Language :: Python :: 2',
37+
'Programming Language :: Python :: 2.7',
38+
'Programming Language :: Python :: 3',
39+
'Programming Language :: Python :: 3.2',
40+
'Programming Language :: Python :: 3.3',
41+
'Programming Language :: Python :: 3.4',
42+
'Programming Language :: Python :: 3.5',
43+
'Programming Language :: Python :: 3.6',
44+
45+
'Topic :: Software Development :: Libraries',
46+
'Topic :: Software Development :: Libraries :: Python Modules'
47+
],
48+
49+
keywords='timer context manager',
50+
51+
py_modules=['timer_cm']
52+
)

timer_cm.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from timeit import default_timer
2+
from decimal import Decimal
3+
4+
class Timer(object):
5+
def __init__(self, name, print_results=True):
6+
self.elapsed = Decimal()
7+
self._name = name
8+
self._print_results = print_results
9+
self._start_time = None
10+
self._children = {}
11+
def __enter__(self):
12+
self.start()
13+
return self
14+
def __exit__(self, *_):
15+
self.stop()
16+
if self._print_results:
17+
self.print_results()
18+
def child(self, name):
19+
try:
20+
return self._children[name]
21+
except KeyError:
22+
result = Timer(name, print_results=False)
23+
self._children[name] = result
24+
return result
25+
def start(self):
26+
self._start_time = self._get_time()
27+
def stop(self):
28+
self.elapsed += self._get_time() - self._start_time
29+
def print_results(self):
30+
print(self._format_results())
31+
def _format_results(self, indent=' '):
32+
result = '%s: %.3fs' % (self._name, self.elapsed)
33+
children = self._children.values()
34+
for child in sorted(children, key=lambda c: c.elapsed, reverse=True):
35+
child_lines = child._format_results(indent).split('\n')
36+
child_percent = child.elapsed / self.elapsed * 100
37+
child_lines[0] += ' (%d%%)' % child_percent
38+
for line in child_lines:
39+
result += '\n' + indent + line
40+
return result
41+
def _get_time(self):
42+
return Decimal(default_timer())

0 commit comments

Comments
 (0)