Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/wa-lead/profiler
Simple python profiler
https://github.com/wa-lead/profiler
optimization profiler python
Last synced: about 1 month ago
JSON representation
Simple python profiler
- Host: GitHub
- URL: https://github.com/wa-lead/profiler
- Owner: Wa-lead
- Created: 2024-02-19T18:03:58.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2024-02-24T13:37:19.000Z (11 months ago)
- Last Synced: 2024-02-24T14:35:03.359Z (11 months ago)
- Topics: optimization, profiler, python
- Language: Python
- Homepage:
- Size: 4.88 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# Profiler
Profiler is an extremely user-friendly Python package designed for profiling Python scripts to analyze their runtime performance with a focus on line-by-line analysis. It leverages the `line_profiler` package to give developers insights into which lines of code consume the most time, helping to optimize and improve code efficiency.
## Installation
To install Profiler, simply use pip:
```
pip install git+https://github.com/wa-lead/profiler.git
```## Usage
After installation, you can use the `profiler` command followed by your script's filename to profile it:
```
profiler script.py
```This will run `script.py` and output a line-by-line breakdown of execution time.
### Function Selection
If you want to selectively profile specific functions within your script, you can use the `--select` option to interactively choose which functions to profile:
```
profiler script.py --select
```Upon using this command, you'll be presented with the list of functions found in `script.py`.
> NOTE: `script.py` must have a `main()` function defined, as Profiler will attempt to profile this function.
### Example
Given a Python script `example.py`, you can profile it as follows:
```python
# Description: This is a simple backtracking algorithm to solve the map coloring problem.def is_valid(coloring, graph, node, color):
for neighbor in graph[node]:
if color == coloring.get(neighbor):
return False
return Truedef color_map(graph, colors, node=0, coloring={}):
if node == len(graph):
return coloringfor color in colors:
if is_valid(coloring, graph, node, color):
coloring[node] = color
result = color_map(graph, colors, node + 1, coloring)
if result is not None:
return coloring
del coloring[node]return None
def main():
# Define the map as a graph: node numbers represent regions, edges represent adjacencies.
graph = {
0: [1, 2, 3],
1: [0, 2, 4],
2: [0, 1, 3, 4],
3: [0, 2, 4],
4: [1, 2, 3]
}
colors = ['Red', 'Green', 'Blue', 'Yellow'] # Define available colorscoloring = color_map(graph, colors)
if coloring is not None:
print("Found a coloring:", coloring)
else:
print("No coloring found that satisfies all constraints.")if __name__ == "__main__":
main()
```In the terminal, run:
```
profiler example.py
```or to interactively select functions:
```
profiler example.py --select
```Output will include a line-by-line breakdown of execution time for each function within `example.py`, helping you identify performance bottlenecks.