
Security News
ECMAScript 2025 Finalized with Iterator Helpers, Set Methods, RegExp.escape, and More
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
timing is a Python timing library providing timer functionality.
This library is one of many providing timers, but aims to:
Some of the library's features are:
pytest
andExample program:
import time
from timing import Timer
timer = Timer(callback=lambda x: print(f"Took {x:.2f} seconds."))
with timer:
print("Executing step 1...")
time.sleep(1)
with timer:
print("Executing step 2...")
time.sleep(2)
Output:
Executing step 1...
Took 1.00 seconds.
Executing step 2...
Took 2.00 seconds.
timing can be installed using
$ python -m pip install timering
Note that on some systems, the Python 3 executable is called python3
instead.
Create a timer, start and stop it:
>>> from timing import Timer
>>> timer = Timer()
>>> timer.start()
>>> timer.stop()
2.442877164
Note that timing.start()
creates a new Timer
, starts and returns it directly.
Create a timer and measure the execution duration of a context:
>>> with timer:
... time.sleep(1)
...
>>> timer.get()
1.001198509
Create a timer and measure a given function:
>>> import time
>>> timer.measure(lambda: time.sleep(2))
>>> timer.get()
2.002377642
Note that timing.measure()
creates a new Timer
and measures the given function with it. To retrieve the result, pass
a callback
to timing.measure()
or use timing.prevtimer.get()
if you are certain it wasn't replaced since the start
of your timing.measure()
call.
Create a timer and wrap a function to measure its execution duration:
>>> timed_func = timer.wrap(lambda: time.sleep(3))
>>> timed_func()
>>> timer.get()
3.003535439
>>> # alternatively
>>> @timer.wrap
... def timed_func():
... time.sleep(2)
...
>>> timed_func()
>>> timer.get()
2.002481228
Note that there is also a timing.wrap()
method available.
To use Python's @decorator
pattern with arguments, libraries often provide functionality for generating decorators:
>>> @timer.wrap()
... def timed_func():
... time.sleep(1)
...
>>> timed_func()
>>> timer.get()
1.001287624
Note that there is also a timing.wrap()
method available.
The Timer
constructor accepts a unit
used for the returned results and a callback
called with the result everytime
the timer stops. Every method also accepts the applicable arguments to override them once.
Be careful when calling stop()
with a unit
when a callback
is defined as the callback
will be called with
the result in the new unit and not the one given to the constructor.
I happily accept feedback and pull requests.
Some ideas:
FAQs
The only timing library you'll ever need.
We found that timering 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.
Security News
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
Security News
A new Node.js homepage button linking to paid support for EOL versions has sparked a heated discussion among contributors and the wider community.
Research
North Korean threat actors linked to the Contagious Interview campaign return with 35 new malicious npm packages using a stealthy multi-stage malware loader.