Advertisement
Guest User

Untitled

a guest
Apr 18th, 2014
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. import timeit
  2.  
  3. def test_slots():
  4. class Obj(object):
  5. __slots__ = ('i', 'l')
  6.  
  7. def __init__(self, i):
  8. self.i = i
  9. self.l = []
  10.  
  11. for i in xrange(1000):
  12. Obj(i)
  13.  
  14. print timeit.Timer('test_slots()', 'from __main__ import test_slots').timeit(10000)
  15.  
  16. timeit(10) - 0.067s
  17. timeit(100) - 0.5s
  18. timeit(1000) - 19.5s
  19. timeit(10000) - ? (probably more than a Game of Thrones episode)
  20.  
  21. import collections
  22. import timeit
  23.  
  24. def test_namedtuples():
  25. Obj = collections.namedtuple('Obj', 'i l')
  26.  
  27. for i in xrange(1000):
  28. Obj(i, [])
  29.  
  30. print timeit.Timer('test_namedtuples()', 'from __main__ import test_namedtuples').timeit(10000)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement