-
-
Notifications
You must be signed in to change notification settings - Fork 526
/
benchmark.py
63 lines (49 loc) · 2.02 KB
/
benchmark.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
52
53
54
55
56
57
58
59
60
61
62
63
"""
As programs increase in size, they have the risk of getting slow as new
features are added and extra layers of complexity to the main features.
Benchmarking is an approach that helps developers use profiling metrics
and their code intuition to optimize programs further. This module uses
cProfile to compare the performance of two functions with each other.
"""
import cProfile
import io
import pstats
import time
# Module-level constants
_SLEEP_DURATION = 0.001
def finish_slower():
"""Finish slower by sleeping more."""
for _ in range(20):
time.sleep(_SLEEP_DURATION)
def finish_faster():
"""Finish faster by sleeping less."""
for _ in range(10):
time.sleep(_SLEEP_DURATION)
def main():
# Create a profile instance
profile = cProfile.Profile()
profile.enable()
for _ in range(2):
finish_slower()
finish_faster()
profile.disable()
# Sort statistics by cumulative time spent for each function call.
# There are other ways to sort the stats by, but this is the most
# common way of doing so. For more info, please consult Python docs:
# https://docs.python.org/3/library/profile.html
buffer = io.StringIO()
ps = pstats.Stats(profile, stream=buffer).sort_stats("cumulative")
# Notice how many times each function was called. In this case, the main
# bottleneck for `finish_slower` and `finish_faster` is `time.sleep`
# which occurred 60 times. By reading the code and the statistics, we
# can infer that 40 occurrences came from `finish_slower` and 20 came
# from `finish_faster`. It is clear why the latter function runs faster
# in this case, but identifying insights like this are not simple in
# large projects. Consider profiling in isolation when analyzing complex
# classes and functions
ps.print_stats()
time_sleep_called = any("60" in line and "time.sleep" in line
for line in buffer.getvalue().split("\n"))
assert time_sleep_called is True
if __name__ == "__main__":
main()