Last active
December 17, 2015 00:29
-
-
Save dogancelik/5521559 to your computer and use it in GitHub Desktop.
(Python) timeit for "check object if it's a list"
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 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
4th method
['a', 1] == list
causes problems in if conditionals. Use the second method (pep8 method).