@reagentx/

Profiler

Python

No description

fork
loading
Files
  • main.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import time
import random
from functools import wraps
from memory_profiler import memory_usage

def profile(fn):
    @wraps(fn)
    def inner(*args, **kwargs):
        fn_kwargs_str = ', '.join(f'{k}={v}' for k, v in kwargs.items())
        print(f'\n{fn.__name__}({fn_kwargs_str})')

        # Measure time
        t = time.perf_counter()
        retval = fn(*args, **kwargs)
        elapsed = time.perf_counter() - t
        print(f'Time   {elapsed:0.4}')

        # Measure memory
        mem, retval = memory_usage((fn, args, kwargs), retval=True, timeout=200, interval=1e-7)

        print(f'Memory {max(mem) - min(mem)}')
        return retval

    return inner


@profile
def no_sort():
    a = []
    for i in range(32678):
        a.append(random.randint(1, 256))
    sum = 0
    for i in a:
        if i >= 128:
            sum += i


@profile
def sort():
    a = []
    for i in range(32678):
        a.append(random.randint(1, 256))
    a.sort()  # the only difference between these functions
    sum = 0
    for i in a:
        if i >= 128:
            sum += i


no_sort()
sort()