-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Benchmark.py
64 lines (49 loc) · 1.61 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
64
import timeit
loops = 1_000_000
val = timeit.timeit("""response("I really don't have anything to say.")""",
"""
def response(hey_bob):
hey_bob = hey_bob.rstrip()
if not hey_bob:
return 'Fine. Be that way!'
is_shout = hey_bob.isupper()
is_question = hey_bob.endswith('?')
if is_shout and is_question:
return "Calm down, I know what I'm doing!"
if is_shout:
return 'Whoa, chill out!'
if is_question:
return 'Sure.'
return 'Whatever.'
""", number=loops) / loops
print(f"if statements: {val}")
val = timeit.timeit("""response("I really don't have anything to say.")""",
"""
def response(hey_bob):
hey_bob = hey_bob.rstrip()
if not hey_bob:
return 'Fine. Be that way!'
is_shout = hey_bob.isupper()
is_question = hey_bob.endswith('?')
if is_shout:
if is_question:
return "Calm down, I know what I'm doing!"
return 'Whoa, chill out!'
if is_question:
return 'Sure.'
return 'Whatever.'
""", number=loops) / loops
print(f"if statements nested: {val}")
val = timeit.timeit("""response("I really don't have anything to say.")""",
"""
ANSWERS = ['Whatever.', 'Sure.', 'Whoa, chill out!',
"Calm down, I know what I'm doing!"]
def response(hey_bob):
hey_bob = hey_bob.rstrip()
if not hey_bob:
return 'Fine. Be that way!'
is_shout = 2 if hey_bob.isupper() else 0
is_question = 1 if hey_bob.endswith('?') else 0
return ANSWERS[is_shout + is_question]
""", number=loops) / loops
print(f"answer list: {val}")