Skip to content

Instantly share code, notes, and snippets.

@dhilst
Last active July 2, 2019 04:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhilst/2e6f02f4a0eff26581f1a6919330ae35 to your computer and use it in GitHub Desktop.
Save dhilst/2e6f02f4a0eff26581f1a6919330ae35 to your computer and use it in GitHub Desktop.
>>> from dis import dis
>>> dis('"foo" in {"bar", "tar"}')
1 0 LOAD_CONST 0 ('foo')
2 LOAD_CONST 1 (frozenset({'bar', 'tar'}))
4 COMPARE_OP 6 (in)
6 RETURN_VALUE
>>> dis('"foo" == "bar" or "foo" == "tar"')
1 0 LOAD_CONST 0 ('foo')
2 LOAD_CONST 1 ('bar')
4 COMPARE_OP 2 (==)
6 JUMP_IF_TRUE_OR_POP 14
8 LOAD_CONST 0 ('foo')
10 LOAD_CONST 2 ('tar')
12 COMPARE_OP 2 (==)
>> 14 RETURN_VALUE
>>>
>>> from timeit import timeit
>>> timeit('"foo" in {"bar", "tar"}')
0.026380221999716014
>>> timeit('"foo" == "bar" or "foo" == "tar"')
0.061998130004212726
>>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment