2022-11-15 06:23:36 -06:00
|
|
|
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!'
|
2022-11-17 04:05:24 -06:00
|
|
|
is_shout = hey_bob.isupper()
|
|
|
|
|
is_question = hey_bob.endswith('?')
|
|
|
|
|
if is_shout and is_question:
|
2022-11-15 06:23:36 -06:00
|
|
|
return "Calm down, I know what I'm doing!"
|
2022-11-17 04:05:24 -06:00
|
|
|
if is_shout:
|
2022-11-15 06:23:36 -06:00
|
|
|
return 'Whoa, chill out!'
|
2022-11-17 04:05:24 -06:00
|
|
|
if is_question:
|
2022-11-15 06:23:36 -06:00
|
|
|
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!'
|
2022-11-17 04:05:24 -06:00
|
|
|
is_shout = hey_bob.isupper()
|
|
|
|
|
is_question = hey_bob.endswith('?')
|
|
|
|
|
if is_shout:
|
|
|
|
|
if is_question:
|
2022-11-15 06:23:36 -06:00
|
|
|
return "Calm down, I know what I'm doing!"
|
|
|
|
|
else:
|
2022-11-17 04:05:24 -06:00
|
|
|
return 'Whoa, chill out!'
|
|
|
|
|
if is_question:
|
2022-11-15 06:23:36 -06:00
|
|
|
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.")""",
|
|
|
|
|
"""
|
|
|
|
|
|
2022-11-17 04:05:24 -06:00
|
|
|
ANSWERS = ['Whatever.', 'Sure.', 'Whoa, chill out!',
|
|
|
|
|
"Calm down, I know what I'm doing!"]
|
|
|
|
|
|
2022-11-15 06:23:36 -06:00
|
|
|
|
|
|
|
|
def response(hey_bob):
|
|
|
|
|
hey_bob = hey_bob.rstrip()
|
2022-11-17 04:05:24 -06:00
|
|
|
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]
|
2022-11-15 06:23:36 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
""", number=loops) / loops
|
|
|
|
|
|
|
|
|
|
print(f"answer list: {val}")
|