### Install line_profiler Source: https://github.com/pyutils/line_profiler/blob/main/docs/source/auto/line_profiler.md This snippet shows how to install the line_profiler package using pip. It includes the command for a basic installation and another for installing with all optional dependencies. ```bash pip install line_profiler ``` ```bash pip install line_profiler[all] ``` -------------------------------- ### Install uv and tomlkit Source: https://github.com/pyutils/line_profiler/blob/main/docs/source/manual/examples/example_toml_config.md Installs the necessary packages `uv` and `tomlkit` using pip. These are required for setting up the Python project and manipulating TOML configuration files programmatically. ```bash pip install uv tomlkit ``` -------------------------------- ### Install line_profiler with IPython support Source: https://github.com/pyutils/line_profiler/blob/main/README.rst Installs line_profiler along with a compatible version of IPython, which may be useful for interactive profiling sessions. ```bash pip install line_profiler[ipython] ``` -------------------------------- ### Install line_profiler using pip Source: https://github.com/pyutils/line_profiler/blob/main/README.rst Installs the line_profiler package using pip. This is the standard way to add the profiling tool to your Python environment. ```bash pip install line_profiler ``` -------------------------------- ### Kernprof CLI Usage Example Source: https://github.com/pyutils/line_profiler/blob/main/docs/source/auto/kernprof.md An example of the detailed usage string for the kernprof command-line interface. It lists positional arguments for specifying code to run and various options for configuration. ```text usage: kernprof [-h] [-V] [--config CONFIG] [--no-config] [--line-by-line [Y[es] | N[o] | T[rue] | F[alse] | on | off | 1 | 0]] [--builtin [Y[es] | N[o] | T[rue] | F[alse] | on | off | 1 | 0]] [-s SETUP] [-p {path/to/script | object.dotted.path}[,...]] [--preimports [Y[es] | N[o] | T[rue] | F[alse] | on | off | 1 | 0]] [--prof-imports [Y[es] | N[o] | T[rue] | F[alse] | on | off | 1 | 0]] [-o OUTFILE] [-v] [-q] [--rich [Y[es] | N[o] | T[rue] | F[alse] | on | off | 1 | 0]] [-u UNIT] [--skip-zero [Y[es] | N[o] | T[rue] | F[alse] | on | off | 1 | 0]] [--summarize [Y[es] | N[o] | T[rue] | F[alse] | on | off | 1 | 0]] [-i [OUTPUT_INTERVAL]] {path/to/script | -m path.to.module | -c "literal code"} ... Run and profile a python script or module. positional arguments: {path/to/script | -m path.to.module | -c "literal code"} The python script file, module, or literal code to run args Optional script arguments options: -h, --help show this help message and exit -V, --version show program's version number and exit --config CONFIG Path to the TOML file, from the `tool.line_profiler.kernprof` table of which to load defaults for the options. (Default: 'pyproject.toml') --no-config Disable the loading of configuration files other than the default one profiling options: --line-by-line [Y[es] | N[o] | T[rue] | F[alse] | on | off | 1 | 0] Use the line-by-line profiler instead of cProfile. Implies `--builtin`. (Default: False; short form: -l) --builtin [Y[es] | N[o] | T[rue] | F[alse] | on | off | 1 | 0] Put `profile` in the builtins. Use `profile.enable()`/`.disable()` to toggle profiling, `@profile` to decorate functions, or `with profile:` to profile a section of code. (Default: False; short form: -b) -s, --setup SETUP Path to the Python source file containing setup code to execute before the code to profile. (Default: N/A) -p, --prof-mod PROF_MOD List of modules, functions and/or classes to profile specified by their name or path. These profiling targets can be supplied both as comma-separated items, or separately with multiple copies of this flag. Packages are automatically recursed into unless they are specified with `.__init__`. Adding the current script/module profiles the entirety of it. Only works with line profiling (`-l`/`--line-by-line`). (Default: N/A; pass an empty string to clear the defaults (or any `-p` target specified earlier)) ---preimports [Y[es] | N[o] | T[rue] | F[alse] | on | off | 1 | 0] Instead of eagerly importing all profiling targets specified via `-p` and profiling them, only profile those that are directly imported in the profiled code. Only works with line profiling (`-l`/`--line-by-line`). (Default: False) Eagerly import all profiling targets specified via `-p` and profile them, instead of only profiling those that are directly imported in the profiled code. Only works with line profiling (`-l`/`--line-by-line`). (Default: True) --prof-imports [Y[es] | N[o] | T[rue] | F[alse] | on | off | 1 | 0] If the script/module profiled is in `--prof-mod`, autoprofile all its imports. Only works with line profiling (`-l`/`--line- by-line`). (Default: False) output options: -o, --outfile OUTFILE Save stats to OUTFILE. (Default: ``` -------------------------------- ### Decorate function for profiling (Modern Quick Start) Source: https://github.com/pyutils/line_profiler/blob/main/README.rst Demonstrates how to decorate a Python function with `@line_profiler.profile` to enable line-by-line profiling. This approach requires setting an environment variable to activate. ```python import line_profiler @line_profiler.profile def my_function(): # Your function code here pass ``` -------------------------------- ### Run script with kernprof (Legacy Quick Start) Source: https://github.com/pyutils/line_profiler/blob/main/README.rst Shows how to use the `kernprof` script for profiling. This method uses a simple `@profile` decorator and requires running the script via `kernprof` with specific flags for verbose output and line-by-line analysis. ```bash kernprof -lv script_to_profile.py ``` -------------------------------- ### Run script with line_profiler (Modern Quick Start) Source: https://github.com/pyutils/line_profiler/blob/main/README.rst Explains how to run a Python script after decorating functions for profiling. Setting the LINE_PROFILE environment variable triggers the profiling and outputs results to stdout upon script completion. ```bash LINE_PROFILE=1 python your_script.py ``` -------------------------------- ### Create Python Script for Line Profiling Source: https://github.com/pyutils/line_profiler/blob/main/docs/source/manual/examples/example_units.md This snippet shows how to create a Python script named 'script.py' that includes functions for prime number calculation and a main function to execute them. This script is intended to be used with the line_profiler tool. ```bash echo "if 1: from line_profiler import profile @profile def is_prime(n): max_val = n ** 0.5 stop = int(max_val + 1) for i in range(2, stop): if n % i == 0: return False return True def find_primes(size): primes = [] for n in range(size): flag = is_prime(n) if flag: primes.append(n) return primes def main(): print('start calculating') primes = find_primes(10) primes = find_primes(1000) primes = find_primes(100000) print(f'done calculating. Found {len(primes)} primes.') if __name__ == '__main__': main() " > script.py ``` -------------------------------- ### Get help for %lprun IPython magic Source: https://github.com/pyutils/line_profiler/blob/main/docs/source/auto/line_profiler.ipython_extension.md This command retrieves usage help and information for the %lprun IPython magic function. It's a standard way to access documentation for IPython magics directly within an IPython session. ```ipython In [1]: %lprun? ``` -------------------------------- ### Profile Code from Stdin using kernprof Source: https://github.com/pyutils/line_profiler/blob/main/docs/source/manual/examples/example_kernprof.md This example demonstrates how to pipe Python code to `kernprof` for line-by-line profiling. It reads code from stdin, executes it, and immediately views the profiling results. This is useful for dynamically generated code where temporary files are not desired. ```bash # This example doesn't make much sense on its own, but just # imagine if this is a command generating code dynamically echo 'import sys' echo 'from fib import _run_fib, fib_no_cache as fib' echo 'for n in sys.argv[1:]:' echo ' print(f"fib({n})", "=", fib(int(n)))' } | PYTHONPATH="${PYTHONPATH}:${PWD}" kernprof --prof-mod fib._run_fib --line-by-line --view - 10 20 ``` -------------------------------- ### Profile Installed Module with Kernprof Source: https://github.com/pyutils/line_profiler/blob/main/docs/source/manual/examples/example_kernprof.md Execute and profile an installed Python module using `kernprof -m`. This command allows profiling of modules as if they were run directly with `python -m`. Ensure the `PYTHONPATH` is set correctly to include the current directory. ```bash PYTHONPATH="${PYTHONPATH}:${PWD}" \ kernprof --prof-mod fib --line-by-line --view -m \ fib --verbose 10 20 30 ``` -------------------------------- ### Run Python Script with Line Profiler (Unit 1us) Source: https://github.com/pyutils/line_profiler/blob/main/docs/source/manual/examples/example_units.md This demonstrates running the 'script.py' using line_profiler with the timing unit set to microseconds (1e-6). The output will show the time spent in each line of the 'is_prime' function in microseconds. ```bash kernprof -l --unit 1e-6 script.py python -m line_profiler script.py.lprof ``` -------------------------------- ### Run Python Script with Line Profiler (Unit 1ns) Source: https://github.com/pyutils/line_profiler/blob/main/docs/source/manual/examples/example_units.md This demonstrates running the 'script.py' using line_profiler with the timing unit set to nanoseconds (1e-9). The output will show the time spent in each line of the 'is_prime' function in nanoseconds. ```bash kernprof -l --unit 1e-9 script.py python -m line_profiler script.py.lprof ``` -------------------------------- ### Example line_profiler output (Default) Source: https://github.com/pyutils/line_profiler/blob/main/docs/source/manual/examples/example_toml_config.md This is a sample output from the `line_profiler` tool, showing a line-by-line breakdown of a Python function (`fib`). It includes metrics like Line #, Hits, Time, Per Hit, and % Time, providing insights into the performance of each line of code. ```text Line # Hits Time Per Hit % Time Line Contents ============================================================== 5 def fib(n): 6 177 145.1 0.8 42.5 if leq(n, 1): 7 89 29.7 0.3 8.7 return n 8 88 29.1 0.3 8.5 part1 = fib(n - 1) 9 88 27.7 0.3 8.1 part2 = fib(n - 2) 10 88 78.0 0.9 22.8 result = utils.add(part1, part2) 11 88 32.2 0.4 9.4 return result 0.00 seconds - /tmp/tmp.vKpODQr6wndemo_pkg/src/demo_pkg/core.py:5 - fib ``` -------------------------------- ### Run Python Script with Line Profiler (Unit 1s) Source: https://github.com/pyutils/line_profiler/blob/main/docs/source/manual/examples/example_units.md This demonstrates running the 'script.py' using line_profiler with the default timing unit of 1 second. The output will show the time spent in each line of the 'is_prime' function in seconds. ```bash kernprof -l script.py python -m line_profiler script.py.lprof ``` -------------------------------- ### Run Python Script with Line Profiler (Unit 1ms) Source: https://github.com/pyutils/line_profiler/blob/main/docs/source/manual/examples/example_units.md This demonstrates running the 'script.py' using line_profiler with the timing unit set to milliseconds (1e-3). The output will show the time spent in each line of the 'is_prime' function in milliseconds. ```bash kernprof -l --unit 1e-3 script.py python -m line_profiler script.py.lprof ``` -------------------------------- ### Python: GlobalProfiler Show Method Source: https://github.com/pyutils/line_profiler/blob/main/docs/source/auto/line_profiler.explicit_profiler.md Explains the `show()` method of the GlobalProfiler class. This method writes the managed profiler statistics to the enabled outputs. If the implicit setup was triggered, this method is called automatically by `atexit`. ```python def show(): # Write the managed profiler stats to enabled outputs. # If the implicit setup triggered, then this will be called by # atexit. ``` -------------------------------- ### Show Profiling Results with line_profiler.line_profiler.show_text Source: https://github.com/pyutils/line_profiler/blob/main/docs/source/auto/line_profiler.line_profiler.md Demonstrates how to use the show_func utility (which internally uses show_text) to display profiling timings for a given function. It involves preparing fake timings and then calling show_func with these timings and other parameters like filename, start line number, function name, units, and output stream. ```python from line_profiler.line_profiler import show_func import line_profiler import inspect # Use a function in this file as an example func = line_profiler.line_profiler.show_text start_lineno = func.__code__.co_firstlineno filename = func.__code__.co_filename func_name = func.__name__ # Build fake timeings for each line in the example function num_lines = len(inspect.getsourcelines(func)[0]) line_numbers = list(range(start_lineno + 3, start_lineno + num_lines)) timings = [(lineno, idx * 1e13, idx * (2e10 ** (idx % 3))) for idx, lineno in enumerate(line_numbers, start=1)] unit = 1.0 output_unit = 1.0 stream = None stripzeros = False rich = 1 show_func(filename, start_lineno, func_name, timings, unit, output_unit, stream, stripzeros, rich) ``` -------------------------------- ### Get code block from file in Python Source: https://github.com/pyutils/line_profiler/blob/main/docs/source/auto/line_profiler.line_profiler.md The `get_code_block` function retrieves a specific block of code from a file, starting at a given line number. It understands Cython code and relies on the undocumented `inspect.getblock()` function. ```python from line_profiler import get_code_block filename = 'my_script.py' line_number = 10 code_lines = get_code_block(filename, line_number) ``` -------------------------------- ### Run Python Package Entrypoint Source: https://github.com/pyutils/line_profiler/blob/main/docs/source/manual/examples/example_toml_config.md Executes the main entry point of the 'demo_pkg' package using `python -m demo_pkg`. This command runs the `__main__.py` script, demonstrating the package's functionality. ```bash python -m demo_pkg ``` -------------------------------- ### Install Python Extension Module Source: https://github.com/pyutils/line_profiler/blob/main/line_profiler/CMakeLists.txt Installs the compiled C++ module to the correct relative location within the build directory, supporting in-place builds when using tools like `pip install -e`. The `message` command shows the calculated installation destination. ```cmake # Install the C++ module to the correct relative location # (this will be an inplace build if you use `pip install -e`) file(RELATIVE_PATH _install_dest "${CMAKE_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}") message(STATUS "[OURS] _install_dest = ${_install_dest}") install(TARGETS ${module_name} LIBRARY DESTINATION "${_install_dest}/") ``` -------------------------------- ### Create Python Package Structure and Files Source: https://github.com/pyutils/line_profiler/blob/main/docs/source/manual/examples/example_toml_config.md Sets up a temporary directory and initializes a Python package named 'demo_pkg' using `uv init`. It then uses a helper function `codeblock` to create core Python files (`core.py`, `utils.py`, `__main__.py`) within the package structure, defining functions for calculations and program execution. ```bash TEMP_DIR=$(mktemp -d --suffix=demo_pkg) mkdir -p $TEMP_DIR cd $TEMP_DIR uv init --lib --name demo_pkg # helper to prevent indentation errors codeblock(){ echo "$1" | python -c "import sys; from textwrap import dedent; print(dedent(sys.stdin.read()).strip('\n'))" } codeblock " import time from demo_pkg.utils import leq from demo_pkg import utils def fib(n): if leq(n, 1): return n part1 = fib(n - 1) part2 = fib(n - 2) result = utils.add(part1, part2) return result def sleep_loop(n): for _ in range(n): time.sleep(0.01) " > src/demo_pkg/core.py codeblock " def leq(a, b): return a <= b def add(a, b): return a + b " > src/demo_pkg/utils.py codeblock " from demo_pkg import core import uuid def main(): run_uuid = uuid.uuid4() print('The UUID of this run is', run_uuid) print('compute fib 10') result = core.fib(10) print('result', result) print('sleeping 5') core.sleep_loop(5) print('done') if __name__ == '__main__': main() " > src/demo_pkg/__main__.py # Run `uv pip install -e .` to install the project locally: uv pip install -e . ``` -------------------------------- ### Instantiate LineProfiler with functions using Python API Source: https://github.com/pyutils/line_profiler/blob/main/README.rst This Python code demonstrates the programmatic usage of LineProfiler. It shows how to create a LineProfiler instance and pass functions to be profiled directly to the constructor. ```python profile = LineProfiler(f, g) ``` -------------------------------- ### __call__(func) Source: https://github.com/pyutils/line_profiler/blob/main/docs/source/auto/line_profiler.md Decorates a function or other callable to automatically start the profiler on entry and stop it on exit. ```APIDOC ## __call__(func) ### Description Decorate a function, method, [`property`](https://docs.python.org/3/library/functions.html#property), [`partial()`](https://docs.python.org/3/library/functools.html#functools.partial) object etc. to start the profiler on function entry and stop it on function exit. ### Method `__call__` ``` -------------------------------- ### Enable Profiling - enable() Source: https://github.com/pyutils/line_profiler/blob/main/docs/source/auto/line_profiler._line_profiler.md Enables the line profiler to start collecting profiling data. This is a core function for initiating the profiling process. ```python profiler.enable() ``` -------------------------------- ### Get Profiling Statistics in Python Source: https://github.com/pyutils/line_profiler/blob/main/docs/source/auto/line_profiler.md The `get_stats` function retrieves the current profiling statistics collected by the line profiler. This data can then be processed, displayed, or saved to a file. ```python def get_stats(): """ Retrieve the current profiling statistics. Returns: LineStats: An object containing the collected profiling data. """ pass ``` -------------------------------- ### Profile with TOML-configured line-profiler Source: https://github.com/pyutils/line_profiler/blob/main/docs/source/manual/examples/example_toml_config.md Runs `kernprof -m demo_pkg` again after updating `pyproject.toml`. This execution uses the new defaults, providing a detailed line-by-line breakdown of the profiled code within the 'demo_pkg'. ```bash # Now, running kernprof uses the new defaults kernprof -m demo_pkg ``` -------------------------------- ### Get Profiling Statistics - get_stats() Source: https://github.com/pyutils/line_profiler/blob/main/docs/source/auto/line_profiler._line_profiler.md Retrieves the collected profiling statistics. This method returns a LineStats object containing detailed timing information for each line of profiled code. ```python stats = profiler.get_stats() ``` -------------------------------- ### Get Python Executable Path (Python) Source: https://github.com/pyutils/line_profiler/blob/main/docs/source/auto/line_profiler.cli_utils.md Returns the path to the currently executing Python interpreter. This is useful for ensuring that subprocesses are launched with the correct Python environment. It directly leverages `sys.executable`. ```python def get_python_executable(): """Return command or path thereto corresponding to [`sys.executable`](https://docs.python.org/3/library/sys.html#sys.executable). * **Returns:** command : Command or path thereto corresponding to [`sys.executable`](https://docs.python.org/3/library/sys.html#sys.executable). * **Return type:** [str](https://docs.python.org/3/library/stdtypes.html#str) """ pass ``` -------------------------------- ### Get CLI Configuration (Python) Source: https://github.com/pyutils/line_profiler/blob/main/docs/source/auto/line_profiler.cli_utils.md Retrieves configuration settings for the line_profiler tool from a TOML file. It normalizes keys from 'some-key' to 'some_key' for easier access. This function is essential for loading user-defined settings for the CLI. ```python def get_cli_config(subtable, *args, **kwargs): """Get the `tool.line_profiler.` configs and normalize its keys (`some-key` -> `some_key`). * **Parameters:** * **subtable** (*str*) – Name of the subtable the CLI app should refer to (e.g. `'kernprof'`) * ***args, **kwargs** – Passed to [`line_profiler.toml_config.ConfigSource.from_config()`](line_profiler.toml_config.md#line_profiler.toml_config.ConfigSource.from_config) * **Returns:** New [`ConfigSource`](line_profiler.toml_config.md#line_profiler.toml_config.ConfigSource) instance """ pass ``` -------------------------------- ### Profile Python Package with kernprof Source: https://github.com/pyutils/line_profiler/blob/main/docs/source/manual/examples/example_toml_config.md Runs the `kernprof` tool to profile the 'demo_pkg' package. Initially, it uses default `kernprof` behavior. The output shows a high-level overview of function execution times. ```bash kernprof -m demo_pkg ``` -------------------------------- ### Python: GlobalProfiler Call Method Source: https://github.com/pyutils/line_profiler/blob/main/docs/source/auto/line_profiler.explicit_profiler.md Details the `__call__` method of the GlobalProfiler class, which is responsible for decorating a function to start the profiler on entry and stop it on exit if the profiler is enabled. If disabled, it returns the original function. ```python def __call__(func): # If the global profiler is enabled, decorate a function to start the # profiler on function entry and stop it on function exit. Otherwise # return the input. # Parameters: # func (Callable): the function to profile # Returns: # a potentially wrapped function # Return type: # Callable ``` -------------------------------- ### Kernprof Command-Line Help Source: https://github.com/pyutils/line_profiler/blob/main/docs/source/auto/kernprof.md Displays the full help message for the kernprof command, outlining all available options for profiling and output configuration. This is useful for understanding the various parameters that can be passed. ```bash kernprof --help ``` -------------------------------- ### Profile Classes and Modules with LineProfiler API Source: https://context7.com/pyutils/line_profiler/llms.txt Profile all functions within a class or module using the LineProfiler API without decorating each function individually. Scoping policies and wrapping options can control what gets profiled. ```python import line_profiler profiler = line_profiler.LineProfiler() # Profile all methods in a class class DataProcessor: def __init__(self, data): self.data = data def transform(self): return [x * 2 for x in self.data] def filter_positive(self): return [x for x in self.data if x > 0] def aggregate(self): return sum(self.data) profiler.add_class(DataProcessor) # Profile all functions in a module import mymodule profiler.add_module(mymodule) # Profile with scoping policy to control what gets profiled from line_profiler.scoping_policy import ScopingPolicy profiler.add_class( DataProcessor, scoping_policy={ 'func': ScopingPolicy.LOCAL, # Only functions defined in this class 'class': ScopingPolicy.NONE, # Don't recurse into nested classes 'module': ScopingPolicy.NONE # Don't follow module references }, wrap=True # Replace methods with profiling wrappers ) # Execute profiled code processor = DataProcessor([1, -2, 3, -4, 5]) profiler.enable_by_count() result = processor.transform() result = processor.filter_positive() total = processor.aggregate() profiler.disable_by_count() profiler.print_stats() ``` -------------------------------- ### line_profiler CLI: View .lprof Files (Bash) Source: https://context7.com/pyutils/line_profiler/llms.txt This section explains how to use the `line_profiler` module from the command line to view and analyze `.lprof` files. It covers basic viewing, applying rich formatting, showing summaries, sorting results, skipping zero-hit lines, and combining multiple options. It also demonstrates how to specify custom output time units and view combined profile files. ```bash # Basic viewing of profile results python -m line_profiler profile_output.lprof # Rich formatting with syntax highlighting python -m line_profiler -r profile_output.lprof # Show summary of total time per function python -m line_profiler -m profile_output.lprof # Sort functions by total time python -m line_profiler -t profile_output.lprof # Skip functions that weren't called python -m line_profiler -z profile_output.lprof # Combine multiple options python -m line_profiler -rmtz profile_output.lprof # Custom output time unit (in seconds, e.g., milliseconds) python -m line_profiler -u 0.001 profile_output.lprof # View multiple profile files combined python -m line_profiler run1.lprof run2.lprof run3.lprof ``` -------------------------------- ### Create Demo Python Script for Profiling Source: https://github.com/pyutils/line_profiler/blob/main/docs/source/auto/line_profiler.md This Python script, 'demo_primes.py', is designed to be profiled using line_profiler. It includes functions for checking prime numbers and finding primes within a given range, decorated with the '@profile' decorator. ```python from line_profiler import profile @profile def is_prime(n): ''' Check if the number "n" is prime, with n > 1. Returns a boolean, True if n is prime. ''' max_val = n ** 0.5 stop = int(max_val + 1) for i in range(2, stop): if n % i == 0: return False return True @profile def find_primes(size): primes = [] for n in range(size): flag = is_prime(n) if flag: primes.append(n) return primes @profile def main(): print('start calculating') primes = find_primes(100000) print(f'done calculating. Found {len(primes)} primes.') if __name__ == '__main__': main() ``` -------------------------------- ### Get a sub-configuration from ConfigSource (Python) Source: https://github.com/pyutils/line_profiler/blob/main/docs/source/auto/line_profiler.toml_config.md Retrieves a nested configuration table from an existing ConfigSource instance. Allows specifying headers to navigate the configuration dictionary. Supports options to allow absence of keys and to create a deep copy of the sub-configuration. ```python from line_profiler.toml_config import ConfigSource default = ConfigSource.from_default() # Get a specific sub-configuration tool_config = default.get_subconfig('tool', 'line_profiler') # Get a nested sub-configuration show_column_widths = default.get_subconfig('show', 'column_widths', copy=True) # Get a sub-configuration, allowing absence optional_section = default.get_subconfig('optional_section', allow_absence=True) ``` -------------------------------- ### Configure line_profiler with pyproject.toml (Bash) Source: https://github.com/pyutils/line_profiler/blob/main/docs/source/manual/examples/example_toml_config.md This snippet shows how to update the `pyproject.toml` file to configure the `line_profiler`. It enables line-by-line profiling, rich output, and specifies modules to profile, such as `demo_pkg.core.fib`. This ensures `kernprof` uses these new defaults. ```bash update_pyproject_toml " # New Config [tool.line_profiler.kernprof] line-by-line = true rich = true verbose = 0 skip-zero = true prof-mod = ['demo_pkg.core.fib'] " ``` -------------------------------- ### Line Profiler Command-Line Interface (CLI) Source: https://github.com/pyutils/line_profiler/blob/main/docs/source/auto/line_profiler.md Provides the main entry point for the line_profiler's command-line interface. It is typically used in conjunction with the 'kernprof -l' command to view profiling output. ```Python import line_profiler # This function is intended to be called from the command line, # typically after running a script with 'kernprof -l'. # Example usage (from terminal): # python -m line_profiler your_script.py line_profiler.main() ``` -------------------------------- ### Run line_profiler with specific options (Bash) Source: https://github.com/pyutils/line_profiler/blob/main/docs/source/manual/examples/example_toml_config.md This command directly invokes `line_profiler` to analyze a profile data file (`.lprof`). The options `-rmtz` correspond to: `r` (rich output), `m` (module), `t` (timer), and `z` (skip zero hits). It targets the `demo_pkg.lprof` file. ```bash python -m line_profiler -rmtz demo_pkg.lprof ``` -------------------------------- ### Configure IPython to automatically load line_profiler Source: https://github.com/pyutils/line_profiler/blob/main/README.rst This Python configuration code is added to IPython's configuration file to automatically load the line_profiler extension on startup. This makes the %lprun magic command available in every IPython session. ```python c.TerminalIPythonApp.extensions = [ 'line_profiler', ] ``` -------------------------------- ### Create and Run Demo Script for Line Profiler Source: https://github.com/pyutils/line_profiler/blob/main/docs/source/auto/line_profiler.explicit_profiler.md This code snippet demonstrates how to create a Python script (`demo.py`) with functions decorated by `@profile`. It then shows how to run this script under various conditions to enable or disable line profiling, including default execution, using `kernprof`, command-line arguments (`--line-profile`), and environment variables (`LINE_PROFILE=1`). ```bash # Write demo python script to disk python -c "if 1:\n import textwrap text = textwrap.dedent( ''' from line_profiler import profile @profile def plus(a, b): return a + b @profile def fib(n): a, b = 0, 1 while a < n: a, b = b, plus(a, b) @profile def main(): import math import time start = time.time() print('start calculating') while time.time() - start < 1: fib(10) math.factorial(1000) print('done calculating') main() ''' ).strip() with open('demo.py', 'w') as file: file.write(text) " echo "---" echo "## Base Case: Run without any profiling" python demo.py echo "---" echo "## Option 0: Original Usage" python -m kernprof -l demo.py python -m line_profiler -rmt demo.py.lprof echo "---" echo "## Option 1: Enable profiler with the command line" python demo.py --line-profile echo "---" echo "## Option 1: Enable profiler with an environment variable" LINE_PROFILE=1 python demo.py ``` -------------------------------- ### Run kernprof with TOML Configuration Source: https://context7.com/pyutils/line_profiler/llms.txt After configuring pyproject.toml, run kernprof without flags to automatically load the settings. Command-line flags can override specific TOML configurations. ```bash # After configuring pyproject.toml, run kernprof without flags: kernprof -m mypackage # The configuration is automatically loaded from pyproject.toml # Override specific settings via command line: kernprof --no-config -m mypackage # Disable config loading kernprof --config custom.toml -m mypackage # Use custom config file ``` -------------------------------- ### LineStats Class Initialization and File Loading in Python Source: https://github.com/pyutils/line_profiler/blob/main/docs/source/auto/line_profiler.md The `LineStats` class holds profiling data. It can be initialized directly with timing data and a unit, or instances can be loaded from files using `from_files` or combined from existing `LineStats` objects using `from_stats_objects`. ```python class LineStats: def __init__(self, timings, unit): """ Initializes LineStats with timing data and time unit. Args: timings (dict): A dictionary where keys are (filename, lineno, funcname) tuples and values are lists of (hits, time, line_time) tuples. unit (float): The time unit for the timings (e.g., 1e-6 for microseconds). """ pass @classmethod def from_files(cls, file, *files): """ Utility function to load an instance from the given filenames. Args: file (str): The first filename to load statistics from. *files (str): Additional filenames to load statistics from. Returns: LineStats: A LineStats object combined from the specified files. """ pass @classmethod def from_stats_objects(cls, stats, *more_stats): """ Combines multiple LineStats objects into a single one. Args: stats (LineStats): The first LineStats object. *more_stats (LineStats): Additional LineStats objects to combine. Returns: LineStats: A new LineStats object containing combined data. """ pass ``` -------------------------------- ### %lprun Magic Command: IPython/Jupyter Profiling (Python) Source: https://context7.com/pyutils/line_profiler/llms.txt This section demonstrates the `%lprun` magic command for profiling specific functions within an IPython or Jupyter environment. It covers loading the extension, profiling single or multiple functions, profiling entire modules, saving results to files, and returning the profiler object. Options for custom time units and skipping zero-hit lines are also included. ```python # In IPython or Jupyter notebook: # Load the extension %load_ext line_profiler # Define functions to profile def slow_function(n): total = 0 for i in range(n): total += i ** 2 return total def helper_function(x): return x * 2 # Profile specific function(s) with -f flag %lprun -f slow_function slow_function(10000) # Profile multiple functions %lprun -f slow_function -f helper_function slow_function(10000) # Profile all functions in a module with -m flag import mymodule %lprun -m mymodule mymodule.main() # Save raw results to file with -D flag %lprun -D output.lprof -f slow_function slow_function(10000) # Save text output to file with -T flag %lprun -T output.txt -f slow_function slow_function(10000) # Return the LineProfiler object with -r flag prof = %lprun -r -f slow_function slow_function(10000) print(prof.get_stats()) # Custom time unit with -u flag (in seconds) %lprun -u 0.001 -f slow_function slow_function(10000) # Skip zero-hit lines with -z flag %lprun -z -f slow_function slow_function(10000) ``` -------------------------------- ### LineProfiler Usage Example - Python Source: https://github.com/pyutils/line_profiler/blob/main/docs/source/auto/line_profiler.line_profiler.md Demonstrates how to use the LineProfiler class to profile a function. It involves creating a LineProfiler instance, decorating a target function with it, calling the function, and then printing the profiling statistics. This is useful for identifying performance bottlenecks within a function. ```python import line_profiler profile = line_profiler.LineProfiler() @profile def func(): x1 = list(range(10)) x2 = list(range(100)) x3 = list(range(1000)) func() profile.print_stats() ``` -------------------------------- ### Python Code for Fibonacci Calculation Source: https://github.com/pyutils/line_profiler/blob/main/docs/source/manual/examples/example_kernprof.md A Python script (`fib.py`) that calculates Fibonacci numbers with and without caching using `functools.lru_cache`. It includes argument parsing for options like verbose output and disabling the cache. This script is used as an example for profiling with `kernprof`. ```python import functools import sys from argparse import ArgumentParser from typing import Callable, Optional, Sequence @functools.lru_cache() def fib(n: int) -> int: return _run_fib(fib, n) def fib_no_cache(n: int) -> int: return _run_fib(fib_no_cache, n) def _run_fib(fib: Callable[[int], int], n: int) -> int: if n < 0: raise ValueError(f'{n = !r}: expected non-negative integer') if n < 2: return 1 prev_prev = fib(n - 2) prev = fib(n - 1) return prev_prev + prev def main(args: Optional[Sequence[str]] = None) -> None: parser = ArgumentParser() parser.add_argument('n', nargs='+', type=int) parser.add_argument('--verbose', action='store_true') parser.add_argument('--no-cache', action='store_true') arguments = parser.parse_args(args) pattern = 'fib({!r}) = {!r}' if arguments.verbose else '{1!r}' func = fib_no_cache if arguments.no_cache else fib for n in arguments.n: result = func(n) print(pattern.format(n, result)) if __name__ == '__main__': main() ``` -------------------------------- ### Kernprof Execution Options Source: https://github.com/pyutils/line_profiler/blob/main/docs/source/auto/kernprof.md Demonstrates different ways to execute Python code with kernprof for profiling. These options allow running modules, literal code strings, or code passed via standard input. ```bash kernprof -m some.module kernprof -c "some code" kernprof - ``` -------------------------------- ### Decorate Function with LineProfiler - Python Source: https://github.com/pyutils/line_profiler/blob/main/docs/source/auto/line_profiler.line_profiler.md The __call__ method of the LineProfiler class acts as a decorator. When applied to a function, method, property, or partial object, it automatically starts the profiler upon function entry and stops it upon exit, enabling line-by-line profiling. ```python import line_profiler profile = line_profiler.LineProfiler() @profile def my_function(): # Code to be profiled pass my_function() profile.print_stats() ``` -------------------------------- ### Run ProfmodExtractor to Get Imports for Profiling Source: https://github.com/pyutils/line_profiler/blob/main/docs/source/auto/line_profiler.autoprofile.profmod_extractor.md Executes the ProfmodExtractor to map specified module imports within an abstract syntax tree. It returns a dictionary where keys are the indices of the imports in the AST and values are their aliases or original names. This is used to identify which imports should be profiled. ```python from line_profiler.autoprofile.profmod_extractor import ProfmodExtractor import ast tree = ast.parse("import os\nfrom sys import path as sys_path") script_file = "/path/to/your/script.py" prof_mod = ["os", "sys.path"] extractor = ProfmodExtractor(tree, script_file, prof_mod) tree_imports_to_profile_dict = extractor.run() print(tree_imports_to_profile_dict) ``` -------------------------------- ### Initialize and Use LineProfiler in Python Source: https://github.com/pyutils/line_profiler/blob/main/docs/source/auto/line_profiler._line_profiler.md Demonstrates how to initialize a LineProfiler instance, wrap a function for profiling, execute the wrapped function, and then print the collected statistics. This snippet is useful for understanding the basic workflow of profiling a Python function. ```python import copy import line_profiler # Create a LineProfiler instance self = line_profiler.LineProfiler() # Wrap a function copy_fn = self(copy.copy) # Call the function copy_fn(self) # Inspect internal properties print(self.functions) print(self.c_last_time) print(self.c_code_map) print(self.code_map) print(self.last_time) # Print stats self.print_stats() ``` -------------------------------- ### Enable Line Profiler In-Code with Custom Output Source: https://github.com/pyutils/line_profiler/blob/main/docs/source/auto/line_profiler.explicit_profiler.md This example demonstrates how to enable line profiling directly within the Python script using `profile.enable()`. It configures the profiler to use a custom output prefix ('customized') and decorates a `fib` function with `@profile`. The script then calls the decorated function. ```bash # In-code enabling python -c "if 1:\n import textwrap text = textwrap.dedent( ''' from line_profiler import profile profile.enable(output_prefix='customized') @profile def fib(n): a, b = 0, 1 while a < n: a, b = b, a + b fib(100) ''' ).strip() with open('demo.py', 'w') as file: file.write(text) " python demo.py ``` -------------------------------- ### Using LineProfiler Class for Profiling in Python Source: https://context7.com/pyutils/line_profiler/llms.txt Demonstrates how to use the LineProfiler class for line-by-line profiling. It shows instantiation, using functions as decorators, adding functions programmatically, enabling/disabling profiling, printing stats, saving to a file, and retrieving raw stats. ```python import line_profiler # Create a profiler instance profiler = line_profiler.LineProfiler() # Method 1: Use as a decorator @profiler def calculate_primes(n): primes = [] for num in range(2, n): is_prime = True for i in range(2, int(num ** 0.5) + 1): if num % i == 0: is_prime = False break if is_prime: primes.append(num) return primes # Method 2: Add functions after creation def fibonacci(n): if n <= 1: return n return fibonacci(n - 1) + fibonacci(n - 2) profiler.add_function(fibonacci) # Method 3: Use as context manager with enable/disable profiler.enable_by_count() result = calculate_primes(100) fib_result = fibonacci(10) profiler.disable_by_count() # Display results to stdout profiler.print_stats() # Save results to file for later analysis profiler.dump_stats('profile_output.lprof') # Get raw stats programmatically stats = profiler.get_stats() print(f"Timer unit: {stats.unit}") print(f"Functions profiled: {len(stats.timings)}") ```