Created
June 22, 2017 22:21
-
-
Save cdgriffith/fa48d9b7032f848ddd5c37c3df35dbb7 to your computer and use it in GitHub Desktop.
Python EAFP vs LBYL speeds
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import reusables | |
@reusables.time_it() | |
def test_lbyl(messages): | |
out = [] | |
for _ in range(10000): | |
if messages and messages[0] and len(messages[0]) >= 3: | |
out.append(messages[0][2]) | |
return out | |
@reusables.time_it() | |
def test_eafp(messages): | |
out = [] | |
for _ in range(10000): | |
try: | |
out.append(messages[0][2]) | |
except IndexError: | |
pass | |
return out | |
if __name__ == '__main__': | |
messages = [["hi there", "how are you?"]] | |
assert len(test_lbyl(messages)) == len(test_eafp(messages)) | |
messages[0].append("I'm doing fine") | |
assert len(test_lbyl(messages)) == len(test_eafp(messages)) | |
# LBYL is faster if the list isn't long enough, avoid an error and an attmepted append statement | |
# Function 'test_lbyl' took a total of 0.0016206308322832311 seconds - args: ([['hi there', 'how are you?']],) | |
# Function 'test_eafp' took a total of 0.0030271251617317893 seconds - args: ([['hi there', 'how are you?']],) | |
# EAFP is faster when the success path is more common, avoiding the costly lookups | |
# Function 'test_lbyl' took a total of 0.0027111909965087614 seconds - args: ([['hi there', 'how are you?', "I'm doing fine"]],) | |
# Function 'test_eafp' took a total of 0.001411011977187686 seconds - args: ([['hi there', 'how are you?', "I'm doing fine"]],) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment