Guest User

Untitled

a guest
Jan 20th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1. """
  2. List allocation with teh same value using different techniques
  3.  
  4. The winner is [value] * times by between 15-25 times
  5. """
  6.  
  7. from timeit import timeit
  8. from itertools import repeat
  9.  
  10.  
  11. SIZE = 10_000
  12. INIT_VALUE = 0
  13.  
  14.  
  15. def first():
  16. """Using itertools.repeat"""
  17. return [x for x in repeat(INIT_VALUE, SIZE)]
  18.  
  19.  
  20. def second():
  21. """Using range"""
  22. return [INIT_VALUE for x in range(SIZE)]
  23.  
  24.  
  25. def third():
  26. return [INIT_VALUE] * SIZE
  27.  
  28.  
  29. for x in [first, second, third]:
  30. t = timeit(stmt=x, number=1_000)
  31. print(f"{x.__name__}: {t}")
  32.  
  33. # first: 0.7676357779999999
  34. # second: 1.0797578159999999
  35. # third: 0.04752255700000019
Add Comment
Please, Sign In to add comment