You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: CHANGELOG.md
+5-1Lines changed: 5 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,11 +2,15 @@
2
2
3
3
All notable changes to this project will be documented in this file.
4
4
5
-
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
5
+
The format is based on [Keep a Changelog](https://keepachangelog1.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
7
7
8
8
## [Unreleased]
9
9
10
+
### Added
11
+
12
+
- Attributes that can be referenced in the `text` template string (suggested by [@mlisovyi](https://github.com/mlisovyi) in [#24]).
13
+
10
14
### Changed
11
15
12
16
-`Timer.timers` changed from regular to `dict` to a custom dictionary supporting basic statistics for named timers.
Copy file name to clipboardExpand all lines: README.md
+41Lines changed: 41 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -57,12 +57,38 @@ You can use `codetiming.Timer` in several different ways:
57
57
58
58
You can turn off explicit reporting of the elapsed time by setting `logger=None`.
59
59
60
+
In the template text, you can also use explicit attributes to refer to the `name` of the timer, or log the elapsed time in`milliseconds`, `seconds` (the default), or`minutes`. For example:
Note that the strings used by `text` are **not** f-strings. Instead they are used as templates that will be populated using `.format()` behind the scenes. If you want to combine the `text` template with an f-string, you need to use double braces for the template values:
68
+
69
+
```python
70
+
t = Timer(text=f"{__file__}: {{:.4f}}")
71
+
```
72
+
73
+
74
+
## Capturing the Elapsed Time
75
+
60
76
When using `Timer` as a class, you can capture the elapsed time when calling `.stop()`:
61
77
62
78
```python
63
79
elapsed_time = t.stop()
64
80
```
65
81
82
+
You can also find the last measured elapsed time in the `.last` attribute. The following code will have the same effect as the previous example:
83
+
84
+
```python
85
+
t.stop()
86
+
elapsed_time = t.last
87
+
```
88
+
89
+
90
+
## Named Timers
91
+
66
92
Named timers are made available in the class dictionary `Timer.timers`. The elapsed time will accumulate if the same name or same timer is used several times. Consider the following example:
67
93
68
94
```python
@@ -87,6 +113,21 @@ WARNING:root:Time spent: 1.73
87
113
88
114
The example shows how you can redirect the timer output to the logging module. Note that the elapsed time spent in the two different uses of `t` has been accumulated in `Timer.timers`.
89
115
116
+
You can also get simple statistics about your named timers. Continuing from the example above:
117
+
118
+
```python
119
+
>>> Timer.timers.max("example")
120
+
3.5836678670002584
121
+
122
+
>>> Timer.timers.mean("example")
123
+
2.6563487200000964
124
+
125
+
>>> Timer.timers.stdev("example")
126
+
1.311427314335879
127
+
```
128
+
129
+
`timers` support `.count()`, `.total()`, `.min()`, `.max()`, `.mean()`, `.median()`, and `.stdev()`.
0 commit comments