Package: linetimer
linetimer: A small Python class to measure the time taken by indented lines.linetimeris a small Python class to quickly measure the time taken by a block of indented linesInstallationTo install the library, simply type inpip install linetimerin your terminal.UsageThe 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 msYou can alsoname the code blocksyou 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 msAndnestthem: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 msToget the time taken in different units, use theunitparameter:with CodeTimer('Block', unit='h'):
   slow_function()
   
Code block 'Block' took: 2.382 hSupported units are ns, us, ms, s , m, h corresponding to nanoseconds, microseconds, milliseconds, seconds, minutes, hours.If you need toretain 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 touse 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 hIf you need toturn off the printed statements, use thesilent=Trueargumentwith CodeTimer(silent=True):
   slow_function()
   
# There will be no printed outputYou can also usefunction decoratorsyntax, 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 msNow 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=1000000000unit='us' => threshold=1000000unit='ms' => threshold=1000unit='s' => threshold=1unit='m' => threshold=1/60unit='h' => threshold=1/3600from 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 nsIf you like this package,upvote it on StackOverflow.IssuesIf you encounter a problem, create anissue on Github.ContributingTo contribute, pleaseopen an issuefirst and discuss your plan for contributing. Then fork this repository and commita pull-requestwith your changes.