line-profiler-decorator
Wrapper around line-profiler adding a few features as an decorator
Install
pip install line-profiler-decorator
Usage
from time import sleep
from line_profiler_decorator import profiler
@profiler
def slow_function():
print("quick line")
sleep(1) # slow one
sleep(0.3) # not that slow
Output
quick line
Timer unit: 1e-06 s
Total time: 6.52823 s
File: /Users/fabio/projects/line-profiler-decorator/examples/example.py
Function: slow_function at line 5
Line # Hits Time Per Hit % Time Line Contents
==============================================================
5 @profiler(aggregate=True)
6 def slow_function():
7 5 69.0 13.8 0.0 print("quick line")
8 5 5016944.0 1003388.8 76.8 sleep(1) # slow one
9 5 1511218.0 302243.6 23.1 sleep(0.3) # not that slow
Options
Save output to a file
Save the output to a file instead of stdout
@profiler("output_file.txt")
def slow_function():
print("quick line")
sleep(1) # slow one
sleep(0.3) # not that slow
Aggregate results
Aggregate the results of multiple calls to the decorated function
@profiler(aggregate=True)
def slow_function():
print("quick line")
sleep(1) # slow one
sleep(0.3) # not that slow
def run_slow(n=100):
for _ in range(n):
slow_function()
Follow function
from time import sleep
from line_profiler_decorator import profiler
def other_function():
sleep(1) # slow one
sleep(0.3) # not that slow
@profiler(follow=[other_function])
def function_that_calls_other_function(n):
for _ in range(n):
other_function()
Output
In [3]: function_that_calls_other_function(10)
Timer unit: 1e-06 s
Total time: 13.0668 s
File: /Users/fabio/projects/line-profiler-decorator/examples/example.py
Function: other_function at line 17
Line # Hits Time Per Hit % Time Line Contents
==============================================================
17 def other_function():
18 10 10037030.0 1003703.0 76.8 sleep(1) # slow one
19 10 3029725.0 302972.5 23.2 sleep(0.3) # not that slow
Total time: 13.0672 s
File: /Users/fabio/projects/line-profiler-decorator/examples/example.py
Function: function_that_calls_other_function at line 22
Line # Hits Time Per Hit % Time Line Contents
==============================================================
22 @profiler(follow=[other_function])
23 def function_that_calls_other_function(n):
24 11 97.0 8.8 0.0 for _ in range(n):
25 10 13067104.0 1306710.4 100.0 other_function()
Grouped aggregations
Group aggregations using a key function.
key is a function that receives the same arguments as the decorated function and returns a value.
The profile results of each call of the decorated function will be grouped and aggregated together by the value returned by the key function
Example:
from time import sleep
from line_profiler_decorator import profiler
@profiler(aggregate=True, key=lambda x: x % 2 == 0)
def function_with_grouped_aggregations(number):
"""This function runs faster for even numbers."""
if number % 2:
# sleep for 1 second if number is odd
sleep(1)
else:
# sleep for 0.2 second if number is even
sleep(0.2)
sleep(0.5)
def run_function_with_grouped_aggregations(n=10):
for _ in range(n):
function_with_grouped_aggregations(_)
Output
The key function returns value True for even numbers and False for odd numbers.
That way the aggregated results will be grouped into 2 profile results.
Timer unit: 1e-06 s
Total time: 3.533 s
File: /Users/fabio/projects/line-profiler-decorator/examples/example.py
Function: function_with_filtered_aggregations at line 28
Line # Hits Time Per Hit % Time Line Contents
==============================================================
28 @profiler(aggregate=True, key=lambda x: x % 2 == 0)
29 def function_with_filtered_aggregations(number):
30 """This function runs faster for even numbers."""
31 5 17.0 3.4 0.0 if number % 2:
32 # sleep for 1 second if number is odd
33 sleep(1)
34 else:
35 # sleep for 0.2 second if number is even
36 5 1015312.0 203062.4 28.7 sleep(0.2)
37 5 2517674.0 503534.8 71.3 sleep(0.5)
Timer unit: 1e-06 s
Total time: 7.53658 s
File: /Users/fabio/projects/line-profiler-decorator/examples/example.py
Function: function_with_filtered_aggregations at line 28
Line # Hits Time Per Hit % Time Line Contents
==============================================================
28 @profiler(aggregate=True, key=lambda x: x % 2 == 0)
29 def function_with_filtered_aggregations(number):
30 """This function runs faster for even numbers."""
31 5 15.0 3.0 0.0 if number % 2:
32 # sleep for 1 second if number is odd
33 5 5017337.0 1003467.4 66.6 sleep(1)
34 else:
35 # sleep for 0.2 second if number is even
36 sleep(0.2)
37 5 2519231.0 503846.2 33.4 sleep(0.5)
Looking at the results above we can see that the first block only aggregated call with even number
therefore spent no time at line 33. On the other hand, the second block shows the same for odd numbers
where no time was used on even numbers.
Metadata
Release files for line-profiler-decorator 0.0.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| line-profiler-decorator-0.0.6.tar.gz | 4.2 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| line_profiler_decorator-0.0.6-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details |
Total release size: 8.4 kB
Release files / line-profiler-decorator-0.0.6.tar.gz
| Download URL | line-profiler-decorator-0.0.6.tar.gz |
|---|---|
| Size | 4.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5fbc2c4bf053607a47a49591e34182c7b81e1aa2eb441f8821972709060d28eb
|
|
BLAKE2b-256 checksum How to use checksums |
08a793f2ca83ea6a5c984d5747093a4931e8764b4a2bb9e3bdd1ac28b45d66f9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.9.7
|
Release files / line_profiler_decorator-0.0.6-py2.py3-none-any.whl
| Download URL | line_profiler_decorator-0.0.6-py2.py3-none-any.whl |
|---|---|
| Size | 4.1 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
03a20cf26b1cfa14a9805570f3d794e7612ee9cac9efb6c8ab144a08e0a205c1
|
|
BLAKE2b-256 checksum How to use checksums |
6bcbcfdcc6f0b97b06482c30c5251a96a5961331fe324117c770c79b47a735e3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.9.7
|
Security for line-profiler-decorator 0.0.6
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.