Skip to content

Commit

Permalink
Trigger garbage collection before measuring memory usage.
Browse files Browse the repository at this point in the history
  • Loading branch information
JoaoBrlt committed Dec 15, 2022
1 parent ca550a9 commit bc98241
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 21 deletions.
51 changes: 37 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ my_function()
Output:

```
my_function | 312 B | 465.9 ns
my_function | 432 B | 445.2 ns
```

### 2. Profile only the memory usage of a function
Expand All @@ -57,7 +57,7 @@ my_function()
Output:

```
my_function | 312 B
my_function | 432 B
```

### 3. Profile only the execution time of a function
Expand All @@ -79,7 +79,7 @@ my_function()
Output:

```
my_function | 432.3 ns
my_function | 439.3 ns
```

### 4. Change the number of iterations
Expand All @@ -106,7 +106,7 @@ pi(100)
Output:

```
pi | 80 B | 6.674 µs
pi | 168 B | 6.461 µs
```

### 5. Change the time and memory units
Expand All @@ -131,7 +131,7 @@ exponential(8, 100)
Output:

```
exponential | 0.08 kB | 0.005581 ms
exponential | 0.168 kB | 0.005429 ms
```

### 6. Change the time and memory precision
Expand All @@ -153,7 +153,7 @@ average([25, 12, 18, 88, 64, 55, 22])
Output:

```
average | 48 B | 172.2024 ns
average | 120 B | 176.6314 ns
```

### 7. Log the arguments and the result
Expand All @@ -174,7 +174,7 @@ greeting_message("John", coins=5)
Output:

```
greeting_message | John, coins=5 | Hello John! You have 5 coins. | 353 B | 332.6 ns
greeting_message | John, coins=5 | Hello John! You have 5 coins. | 521 B | 350.1 ns
```

### 8. Set a custom name for a function
Expand All @@ -198,7 +198,7 @@ factorial(10)
Output:

```
Naive method | 96 B | 420.2 ns
Naive method | 160 B | 411.3 ns
```

### 9. Compare multiple functions
Expand All @@ -225,11 +225,35 @@ my_function_2(10)
Output:

```
List comprehension | 344 B | 619.6 ns
For loop | 192 B | 664.7 ns
List comprehension | 464 B | 666.8 ns
For loop | 312 B | 650.7 ns
```

### 10. Enable garbage collection during measurements
### 10. Profile a recursive function

The decorators work seamlessly with recursive functions.\
Only one profiling message is logged per function call even if the function is recursive.

```python
from simple_profile import simple_profile

@simple_profile(print_args=True, print_result=True, iterations=100)
def fibonacci(n):
if n <= 1:
return n
else:
return fibonacci(n - 1) + fibonacci(n - 2)

fibonacci(10)
```

Output:

```
fibonacci | 10 | 55 | 1.648 kB | 21.04 µs
```

### 11. Enable garbage collection during measurements

By default, garbage collection is temporarily turned off to make measurements more comparable, but it is possible to enable it if you prefer.\
To do this, you can set the `enable_gc` argument of the `simple_profile()` and `time_profile()` decorators to `True`.
Expand All @@ -252,7 +276,6 @@ my_function_2()
Output:

```
Without GC | 834 B | 667.3 ns
With GC | 834 B | 674.6 ns
Without GC | 954 B | 666.5 ns
With GC | 954 B | 669.2 ns
```

15 changes: 8 additions & 7 deletions simple_profile/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import gc
import timeit
import tracemalloc
from typing import Any, Callable, Optional
Expand All @@ -16,6 +17,7 @@ def measure_memory_usage(
:return: the memory usage of the function call (in bytes) and its result
"""
tracemalloc.start()
gc.collect()
result = function(*args, **kwargs)
memory_usage = tracemalloc.get_traced_memory()
tracemalloc.stop()
Expand Down Expand Up @@ -50,10 +52,11 @@ def measure_execution_time(
:param enable_gc: whether to enable garbage collection during the measurement
:return: the execution time of all the iterations of the function call (in seconds)
"""
setup = "pass"
if enable_gc:
setup = "gc.enable()"
return timeit.timeit(stmt=lambda: function(*args, **kwargs), setup=setup, number=iterations)
return timeit.timeit(
stmt=lambda: function(*args, **kwargs),
setup="gc.enable()" if enable_gc else "pass",
number=iterations
)


def measure_average_execution_time(
Expand Down Expand Up @@ -121,9 +124,7 @@ def select_profile_name(name: Optional[str], function: Callable) -> str:
:param function: the analyzed function
:return: the profile name
"""
if name is not None:
return name
return function.__name__
return name if name is not None else function.__name__


def get_function_call_log(
Expand Down

0 comments on commit bc98241

Please sign in to comment.