A small Python class to quickly measure the time taken while executing a block of indented lines
Project description
linetimer: A small Python class to measure the time taken by indented lines.
linetimer is a small Python class to quickly measure the time taken by a block of indented lines
Installation
To install the library, simply type in pip install linetimer
in your terminal.
Usage
The basic usage is:
from linetimer import CodeTimer
with CodeTimer():
line_to_measure()
another_line()
# etc...
Which will show the following after the indented line(s) finishes executing:
Code block took: x.xxx ms
You can also name the code blocks you want to measure:
with CodeTimer('loop 1'):
for i in range(100000):
pass
with CodeTimer('loop 2'):
for i in range(100000):
pass
Code block 'loop 1' took: 4.991 ms
Code block 'loop 2' took: 3.666 ms
And nest them:
with CodeTimer('Outer'):
for i in range(100000):
pass
with CodeTimer('Inner'):
for i in range(100000):
pass
for i in range(100000):
pass
Code block 'Inner' took: 2.382 ms
Code block 'Outer' took: 10.466 ms
To get time in a different unit, you can do this:
with CodeTimer('Block', unit='h'):
slow_function()
Code block 'Block' took: 2.382 h
Supported units are ms, s , m, h corresponding to milliseconds, seconds, minutes, hours.
If you need to retain the time taken, you can do it with:
ct = CodeTimer()
with ct:
slow_function()
ct.took # This contains the time taken as per the unit provided (milliseconds by default)
Finally, if you need to turn off the printed statements, use the silent=True
argument
with CodeTimer(silent=True):
slow_function()
# There will be no printed output
If you like this package, upvote it on StackOverflow.
Issues
If you encounter a problem, create an issue on Github.
Contributing
To contribute, please open an issue first and discuss your plan for contributing. Then fork this repository and commit a pull-request with your changes.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Hashes for linetimer-0.1.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ec548663eca934ce6d05abbafd4d4bd3d3e841f8261fabce6e20a08afb753a50 |
|
MD5 | a49365ece4a799cb4e39b2ab595043af |
|
BLAKE2b-256 | 35c81b15e4bcbaa31cfcaa09935cf8534e061d2c03d96b081d7438cd7a96697c |
Hashes for linetimer-0.1.2-py2-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 80f448d619e6a73c6b54b14bfe32f4082463dbbdcde6c87e33a4748cebfc0b51 |
|
MD5 | 079aea1228e9581126f4231757223660 |
|
BLAKE2b-256 | 7179b3ef13d2829c65afdedcfe1db859cfe502028c50d49e52ab22646103be1f |