ttictoc
Time execution of blocks of code.
Tested against python 3.6, python 3.7, and 3.8
How to install
From pip
pip install ttictoc
or download this repo and do
pip install .
TicToc
The easiest way to time something is with tic and toc
import time
from ttictoc import tic,toc
tic()
time.sleep(1)
elapsed = toc()
print('Elapsed time:',elapsed)
You can execute multiple tocs in a matlab-like fashon
import time
from ttictoc import tic,toc
tic()
for i in range(2):
tic()
time.sleep(1)
elapsed = toc()
print('[IN LOOP] Elapsed time:',elapsed)
print('[OUT LOOP] Elapsed time:',toc())
Timer Class
It works just like tic,toc.
import time
from ttictoc import Timer
# Simple
t = Timer()
t.start()
time.sleep(1)
elapsed = t.stop()
print('Elapsed time:',elapsed)
# Nested
t.start()
for i in range(2):
t.start()
time.sleep(1)
elapsed = t.stop()
print('[IN LOOP] Elapsed time:',elapsed)
print('[OUT LOOP] Elapsed time:',t.stop())
Context manager
You can also use it as context manager
import time
from ttictoc import Timer
# Default
with Timer():
time.sleep(1)
# With out verbose
with Timer(verbose=False) as T:
time.sleep(1)
print('Elapsed time:',T.elapsed)
# With default verbose message
with Timer(verbose_msg=f'[User msg][{time.time()}] Elapsed time: {{}}'):
time.sleep(1)
Deactivating matlab-like nesting
You can deactivate the matlab-like nesting. In this case calling start will update the global starting time for toc. However, you can have nested tics by giving a key to start and stop.
import time
from ttictoc import Timer,tic2,toc2
tic2()
for i in range(2):
tic2()
time.sleep(1)
elapsed = toc2()
print('[IN LOOP] Elapsed time:',elapsed)
print('[OUT LOOP] Elapsed time:',toc2())
t = Timer(matlab_like=False)
t.start()
time.sleep(1)
t.start() # Restarts the starting point
time.sleep(1)
elapsed = t.stop()
print('Elapsed time:',elapsed) # ~1 second
# Nested
t.start(key='Init')
for i in range(2):
t.start(key=i)
time.sleep(1)
elapsed = t.stop(key=i)
print('[IN LOOP] Elapsed time:',elapsed)
print('[OUT LOOP] Elapsed time:',t.stop('Init'))
print('\n[OUT LOOP][Init] Elapsed time:',t.stop('Init'))
print('[OUT LOOP][0] Elapsed time:',t.stop(0))
print('[OUT LOOP][1] Elapsed time:',t.stop(1))
Specify timing method
By default, Timer (and tic,toc) use timeit.default_timer. However, the timing function can be selected as follow.
import time
from ttictoc import Timer
t = Timer(func_time=time.clock)
t.start()
time.sleep(5)
elapsed = t.stop()
print('Elapsed time:',elapsed)
Release files for ttictoc 0.5.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| ttictoc-0.5.6.tar.gz | 4.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| ttictoc-0.5.6-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 10.1 kB
Release files / ttictoc-0.5.6.tar.gz
| Download URL | ttictoc-0.5.6.tar.gz |
|---|---|
| Size | 4.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9ae0a534faf299b13f7d71693f8f97b28069932e2583effec799b7e8bf1964d9
|
|
BLAKE2b-256 checksum How to use checksums |
57d6491931c4a621bbbc7d3731669be92766c6d5da9219d982b09c495ac9e5f4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.4.2 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.8.2
|
Release files / ttictoc-0.5.6-py3-none-any.whl
| Download URL | ttictoc-0.5.6-py3-none-any.whl |
|---|---|
| Size | 5.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7d7328a19b2fbb8e2b96c246d6e0ecc700370acb89ee74f4a318d596bf96c286
|
|
BLAKE2b-256 checksum How to use checksums |
bb13b47140eaf42e35f38c5dd035781de249cc8a429dcdf94fc10ac937312f71
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.4.2 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.8.2
|