Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
A small Python class to quickly measure the time taken while executing a block of indented lines
linetimer is a small Python class to quickly measure the time taken by a block of indented lines
To install the library, simply type in pip install linetimer
in your terminal.
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,
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.
If you encounter a problem, create an issue on Github.
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.
FAQs
A small Python class to quickly measure the time taken while executing a block of indented lines
We found that linetimer demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.