-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(profiling): Add examples and setup
- Loading branch information
1 parent
2e0b14c
commit d953762
Showing
8 changed files
with
242 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,3 +14,6 @@ htmlcov | |
.venv | ||
|
||
.DS_Store | ||
|
||
*.profile.* | ||
*results.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Profiling Python code | ||
|
||
## Setup | ||
|
||
```bash | ||
python3 -m venv .venv | ||
source .venv/bin/activate | ||
pip install pip --upgrade | ||
pip install pyinstrument snakeviz | ||
``` | ||
|
||
## Sources | ||
|
||
- https://docs.python.org/3/library/timeit.html | ||
- https://docs.python.org/3/library/profile.html | ||
- https://pyinstrument.readthedocs.io/en/latest/home.html | ||
- https://realpython.com/python-profiling/#pyinstrument-take-snapshots-of-the-call-stack | ||
|
||
## Profiling with pyinstrument | ||
|
||
```bash | ||
python -m pyinstrument --color --renderer=html --use-timing-thread --show-all --timeline --outfile _pyinstrument.profile.html src/profiling/primes.py | ||
python -m pyinstrument --color --renderer=html --use-timing-thread --show-all --outfile _pyinstrument.profile.html src/profiling/primes.py | ||
python -m pyinstrument --color --renderer=html --use-timing-thread --show-all --timeline src/profiling/primes.py | ||
python -m pyinstrument src/profiling/primes.py | ||
``` | ||
|
||
## Profiling with cProfile | ||
|
||
```bash | ||
python -m cProfile -o _profile_data.profile.pstats src/profiling/primes.py | ||
python -m snakeviz _profile_data.profile.pstats | ||
``` |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import random | ||
import time | ||
|
||
RESULTS_FILE_NAME = "_results.txt" | ||
UNIQUE_RESULTS_FILE_NAME = "_unique_results.txt" | ||
|
||
|
||
def _slow_sort(arr): | ||
n = len(arr) | ||
for i in range(n): | ||
for j in range(0, n - i - 1): | ||
if arr[j] > arr[j + 1]: | ||
arr[j], arr[j + 1] = arr[j + 1], arr[j] | ||
return arr | ||
|
||
|
||
def sort(*args, **kwargs): | ||
return _slow_sort(*args, **kwargs) | ||
|
||
|
||
def _slow_prime_check(n): | ||
if n < 2: | ||
return False | ||
for i in range(2, int(n**0.5) + 1): | ||
if n % i == 0: | ||
return False | ||
time.sleep(0.01) | ||
return True | ||
|
||
|
||
def prime_check(*args, **kwargs): | ||
return _slow_prime_check(*args, **kwargs) | ||
|
||
|
||
def _slow_make_unique(data): | ||
unique = [] | ||
for item in data: | ||
if item not in unique: | ||
unique.append(item) | ||
return unique | ||
|
||
|
||
def make_unique(*args, **kwargs): | ||
return _slow_make_unique(*args, **kwargs) | ||
|
||
|
||
def _slow_save_items(results, file_name): | ||
for item in results: | ||
with open(file_name, "a") as f: | ||
f.write(str(item) + "\n") | ||
|
||
|
||
def save_items(*args, **kwargs): | ||
return _slow_save_items(*args, **kwargs) | ||
|
||
|
||
def process_data(data): | ||
primes = [] | ||
result = [] | ||
for item in data: | ||
if prime_check(item): | ||
result.append(f"* {str(item).rjust(6)} is prime") | ||
primes.append(item) | ||
else: | ||
result.append(f" {str(item).rjust(6)}") | ||
|
||
save_items(result, file_name=RESULTS_FILE_NAME) | ||
save_items(make_unique(sort(primes)), file_name=UNIQUE_RESULTS_FILE_NAME) | ||
|
||
return result | ||
|
||
|
||
def main(): | ||
with open(RESULTS_FILE_NAME, "w") as f: | ||
f.write("") | ||
with open(UNIQUE_RESULTS_FILE_NAME, "w") as f: | ||
f.write("") | ||
|
||
number = 1000 | ||
data = [random.randint(1, number) for _ in range(number)] | ||
|
||
start_time = time.time() | ||
|
||
sorted_data = sort(data) | ||
processed_data = process_data(sorted_data) | ||
|
||
end_time = time.time() | ||
|
||
print(f"Time taken: {end_time - start_time:.2f}s") | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |