Skip to content

Instantly share code, notes, and snippets.

@dogancelik
Last active December 17, 2015 00:29
Show Gist options
  • Save dogancelik/5521559 to your computer and use it in GitHub Desktop.
Save dogancelik/5521559 to your computer and use it in GitHub Desktop.
(Python) timeit for "check object if it's a list"
import timeit
# Tested on Python 2.7.5 (64-bit)
t = timeit.Timer(stmt="isinstance(['a', 1], types.ListType)", setup="import types")
print t.timeit() # 0.310844602274
t = timeit.Timer(stmt="isinstance(['a', 1], list)")
print t.timeit() # 0.313751077743
t = timeit.Timer(stmt="type(['a', 1]) == list")
print t.timeit() # 0.323928247411
t = timeit.Timer(stmt="['a', 1] == list")
print t.timeit() # 0.263354284041
t = timeit.Timer(stmt="type(['a', 1]) is list")
print t.timeit() # 0.30530935573
@dogancelik
Copy link
Author

4th method ['a', 1] == list causes problems in if conditionals. Use the second method (pep8 method).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment