Skip to main content

A small Python class to quickly measure the time taken while executing a block of indented lines

Project description

Build Status codecov PyPI version

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 the time taken in different units, use the unit parameter:

with CodeTimer('Block', unit='h'):
   slow_function()
   
Code block 'Block' took: 2.382 h

Supported units are ns, us, ms, s , m, h corresponding to nanoseconds, microseconds, 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)

Sometimes you want to use your own dedicated logger, you can do it with:

import logging
my_logger = logging.get_logger('xyz')

with CodeTimer('Block', unit='h', logger_func = my_logger.info):
   slow_function()

This will log to an appropriate handler,

INFO - Code block 'Block' took: 2.382 h

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

You can also use function decorator syntax, like this:

from linetimer import linetimer

@linetimer()
def my_function(a, b):
   pass

my_function('a', 'b')

>>> Code block 'my_function' took x.yz ms

@linetimer(show_args=True) # will print function parameters
def my_function(a, b):
   pass

my_function('a', b='b')

>>> Code block 'my_function('a', b='b') took x.yz ms

Now you can add threshold to log only necessary statements, which have execution time greater than equal to given threshold value.

By default, threshold is set to 'None', i.e., no threshold.

Threshold value is set in specified unit. For e.g., To set threshold value to 1 second,

  • unit='ns' => threshold=1000000000
  • unit='us' => threshold=1000000
  • unit='ms' => threshold=1000
  • unit='s' => threshold=1
  • unit='m' => threshold=1/60
  • unit='h' => threshold=1/3600
from linetimer import linetimer

@linetimer(threshold=None)
def my_function():
   pass

my_function()

>>> Code block 'my_function' took x.yz ms

@linetimer(threshold=1000) # will log only when execution time is >= 1000 milliseconds
def my_function():
   pass

my_function()

>>> Code block 'my_function' took x.yz ms

@linetimer(unit='us', threshold=1000) # will log only when execution time is >= 1000 microseconds
def my_function():
   pass

my_function()

>>> Code block 'my_function' took x.yz us

@linetimer(unit='ns', threshold=1000) # will log only when execution time is >= 1000 nanoseconds
def my_function():
   pass

my_function()

>>> Code block 'my_function' took x.yz ns

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 Distribution

linetimer-0.1.5.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

linetimer-0.1.5-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file linetimer-0.1.5.tar.gz.

File metadata

  • Download URL: linetimer-0.1.5.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.24.0 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.50.2 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.7.6

File hashes

Hashes for linetimer-0.1.5.tar.gz
Algorithm Hash digest
SHA256 f9723860bbbeae05e4e1383b237e5b73f98c71b73e50dc8c2856a0580269752b
MD5 86ce050fe9d2ebb4d732b4f346a33d3d
BLAKE2b-256 e12d5b246f9eff0f75ed4f243dc52f81facdc35596a024051260aa9215e9e2ac

See more details on using hashes here.

File details

Details for the file linetimer-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: linetimer-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.24.0 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.50.2 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.7.6

File hashes

Hashes for linetimer-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 18f917bf9c3afe2ba2375dbf97fd01189c1967fe4c49382036b03201e03c2889
MD5 99842a091db1b9597a23f529218417bf
BLAKE2b-256 76d1c8b30025b3e76d40f6ed4e6f9ef8bd3a61b251cd9b4c2c822749be45a7ac

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page