Skip to content

Commit

Permalink
feat: make code in primes.py slow
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrgredowski committed Oct 17, 2024
1 parent c72624d commit ce1391a
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 24 deletions.
3 changes: 2 additions & 1 deletion src/profiling/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
## Setup

```bash
python3 -m venv .venv
python3 -m venv .venv # You can also use conda or virtualenv, your choice
.venv\Scripts\activate # Windows
# source .venv/bin/activate # Linux/MacOS
# conda activate your-environment-name # Conda
pip install pyinstrument snakeviz
```

Expand Down
25 changes: 2 additions & 23 deletions src/profiling/primes.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,7 @@ def _slow_sort(arr):
return arr


def _fast_sort(arr):
return sorted(arr)


def sort(*args, **kwargs):
return _fast_sort(*args, **kwargs)
return _slow_sort(*args, **kwargs)


Expand All @@ -33,17 +28,8 @@ def _slow_prime_check(n):
return True


def _faster_prime_check(n):
if n < 2:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True


def prime_check(*args, **kwargs):
return _faster_prime_check(*args, **kwargs)
return _slow_prime_check(*args, **kwargs)


def _slow_make_unique(data):
Expand All @@ -64,15 +50,8 @@ def _slow_save_items(results, file_name):
f.write(str(item) + "\n")


def _fast_save_items(results, file_name):
with open(file_name, "a") as f:
for item in results:
f.write(str(item) + "\n")


def save_items(*args, **kwargs):
return _fast_save_items(*args, **kwargs)
# return _slow_save_items(*args, **kwargs)
return _slow_save_items(*args, **kwargs)


def process_data(data):
Expand Down

0 comments on commit ce1391a

Please sign in to comment.