SHARE
TWEET

Untitled

a guest Apr 18th, 2019 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. In [1]: import timeit
  2.  
  3. In [4]: class A(object):
  4.    ...:     pass
  5.    ...:
  6.  
  7. In [5]: item = object()
  8.  
  9. In [6]: item_a = A()
  10.  
  11. In [7]: def descendent_isinstance():
  12.    ...:     return isinstance(item_a, object)
  13.    ...:
  14.  
  15. In [8]: def direct_isinstance():
  16.    ...:     return isinstance(item_a, A)
  17.    ...:
  18.  
  19. In [9]: def direct_isinstance_builtin():
  20.    ...:     return isinstance(item, object)
  21.    ...:
  22.  
  23. In [10]: def not_isinstance():
  24.     ...:     return isinstance(item, A)
  25.     ...:
  26.  
  27. In [11]: descendent_isinstance()
  28. Out[11]: True
  29.  
  30. In [12]: direct_isinstance()
  31. Out[12]: True
  32.  
  33. In [14]: direct_isinstance_builtin()
  34. Out[14]: True
  35.  
  36. In [15]: not_isinstance()
  37. Out[15]: False
  38.  
  39. In [17]: tests = {
  40.     ...:     "descendent_isinstance": descendent_isinstance,
  41.     ...:     "direct_isinstance": direct_isinstance,
  42.     ...:     "direct_isinstance_builtin": direct_isinstance_builtin,
  43.     ...:     "not_isinstance": not_isinstance,
  44.     ...:     "item": item,
  45.     ...:     "item_a": item_a,
  46.     ...:     "A": A,
  47.     ...: }
  48.  
  49. In [18]: timeit.repeat(stmt="descendent_isinstance()", globals=tests)
  50. Out[18]:
  51. [0.18606656999997995,
  52.  0.17976311300003545,
  53.  0.17793933600000855,
  54.  0.17901940199999444,
  55.  0.1791184380000459]
  56.  
  57. In [19]: timeit.repeat(stmt="direct_isinstance()", globals=tests)
  58. Out[19]:
  59. [0.16835473799994816,
  60.  0.16796777699994436,
  61.  0.16732346500009498,
  62.  0.1671851889999516,
  63.  0.1646581120000974]
  64.  
  65. In [20]: timeit.repeat(stmt="direct_isinstance_builtin()", globals=tests)
  66. Out[20]:
  67. [0.17491985099991325,
  68.  0.17345420999993166,
  69.  0.17239584300000388,
  70.  0.1734395940000013,
  71.  0.17397786800006543]
  72.  
  73. In [21]: timeit.repeat(stmt="not_isinstance()", globals=tests)
  74. Out[21]:
  75. [0.19989036399999804,
  76.  0.1967721519999941,
  77.  0.1962455799999816,
  78.  0.19687672300005943,
  79.  0.19701133199998822]
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top