-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofiler.py
More file actions
60 lines (46 loc) · 1.21 KB
/
profiler.py
File metadata and controls
60 lines (46 loc) · 1.21 KB
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
52
53
54
55
56
57
58
59
60
# In The Name Of God
# ========================================
# [] File Name : profiler.py
#
# [] Creation Date : 09-01-2017
#
# [] Created By : Parham Alvani (parham.alvani@gmail.com)
# =======================================
import cProfile
import datetime
import functools
def timer(function):
@functools.wraps(function)
def _timer(*args, **kwargs):
start = datetime.datetime.now()
try:
return function(*args, **kwargs)
finally:
end = datetime.datetime.now()
print("%s: %s" % (function.__name__, end - start))
return _timer
def profiler(function):
@functools.wraps(function)
def _profiler(*args, **kwargs):
profiler = cProfile.Profile()
try:
profiler.enable()
return function(*args, **kwargs)
finally:
profiler.disable()
profiler.print_stats()
return _profiler
@profiler
def profiled_fibonacci(n):
return fibonacci(n)
@timer
def timed_fibonacci(n):
return fibonacci(n)
def fibonacci(n):
if n < 2:
return n
else:
return fibonacci(n - 1) + fibonacci(n - 2)
if __name__ == "__main__":
timed_fibonacci(32)
profiled_fibonacci(32)